Commit baafd121 authored by Utkarsh Ayachit's avatar Utkarsh Ayachit Committed by Andrew Bauer

Avoid duplication in vtkXMLDataSetWriter and vtkXMLCompositeDataWriter.

vtkXMLCompositeDataWriter, instead of duplicating code from
vtkXMLDataSetWriter to create the appropriate type of reader, now simply
call a new method on vtkXMLDataSetWriter to create the writer for
a specific dataset type.

Also avoid hardcoding filename extensions, instead using API on the appropriate
writer for the dataset type to get the extension string.
parent e25114d2
......@@ -342,41 +342,15 @@ vtkStdString vtkXMLPMultiBlockDataWriter::CreatePieceFileName(
std::string fname;
std::string extension;
switch (dataSetType)
if (const char* cext = this->GetDefaultFileExtensionForDataSet(dataSetType))
{
case VTK_POLY_DATA:
{
extension = "vtp";
break;
}
case VTK_STRUCTURED_POINTS:
case VTK_IMAGE_DATA:
case VTK_UNIFORM_GRID:
{
extension = "vti";
break;
extension = cext;
}
case VTK_UNSTRUCTURED_GRID:
{
extension = "vtu";
break;
}
case VTK_STRUCTURED_GRID:
{
extension = "vts";
break;
}
case VTK_RECTILINEAR_GRID:
{
extension = "vtr";
break;
}
default:
else
{
vtkErrorMacro(<<this->Controller->GetLocalProcessId() << " Unknown data set type.");
return fname;
}
}
std::ostringstream fn_with_warning_C4701;
fn_with_warning_C4701
......
......@@ -17,6 +17,7 @@
#include "vtkCallbackCommand.h"
#include "vtkCompositeDataPipeline.h"
#include "vtkCompositeDataSet.h"
#include "vtkDataObjectTreeIterator.h"
#include "vtkErrorCode.h"
#include "vtkExecutive.h"
#include "vtkGarbageCollector.h"
......@@ -33,14 +34,10 @@
#include "vtkStructuredGrid.h"
#include "vtkUnstructuredGrid.h"
#include "vtkXMLDataElement.h"
#include "vtkXMLImageDataWriter.h"
#include "vtkXMLPolyDataWriter.h"
#include "vtkXMLRectilinearGridWriter.h"
#include "vtkXMLStructuredGridWriter.h"
#include "vtkXMLUnstructuredGridWriter.h"
#include "vtkXMLWriter.h"
#include "vtkDataObjectTreeIterator.h"
#include "vtkXMLDataSetWriter.h"
#include <vtksys/SystemTools.hxx>
#include <map>
#include <sstream>
#include <string>
#include <vector>
......@@ -49,12 +46,39 @@
class vtkXMLCompositeDataWriterInternals
{
// These are used to by GetDefaultFileExtension(). This helps us avoid
// creating new instances repeatedly for the same dataset type.
std::map<int, vtkSmartPointer<vtkXMLWriter> > TmpWriters;
public:
std::vector< vtkSmartPointer<vtkXMLWriter> > Writers;
std::string FilePath;
std::string FilePrefix;
vtkSmartPointer<vtkXMLDataElement> Root;
std::vector<int> DataTypes;
// Get the default extension for the dataset_type. Will return NULL if an
// extension cannot be determined.
const char* GetDefaultFileExtensionForDataSet(int dataset_type)
{
std::map<int, vtkSmartPointer<vtkXMLWriter> >::iterator iter
= this->TmpWriters.find(dataset_type);
if (iter == this->TmpWriters.end())
{
vtkSmartPointer<vtkXMLWriter> writer;
writer.TakeReference(vtkXMLDataSetWriter::NewWriter(dataset_type));
if (writer)
{
std::pair<int, vtkSmartPointer<vtkXMLWriter> > pair(dataset_type, writer);
iter = this->TmpWriters.insert(pair).first;
}
}
if (iter != this->TmpWriters.end())
{
return iter->second->GetDefaultFileExtension();
}
return NULL;
}
};
//----------------------------------------------------------------------------
......@@ -79,6 +103,12 @@ vtkXMLCompositeDataWriter::~vtkXMLCompositeDataWriter()
delete this->Internal;
}
//----------------------------------------------------------------------------
const char* vtkXMLCompositeDataWriter::GetDefaultFileExtensionForDataSet(int dataset_type)
{
return this->Internal->GetDefaultFileExtensionForDataSet(dataset_type);
}
//----------------------------------------------------------------------------
unsigned int vtkXMLCompositeDataWriter::GetNumberOfDataTypes()
{
......@@ -370,8 +400,7 @@ void vtkXMLCompositeDataWriter::FillDataTypes(vtkCompositeDataSet* hdInput)
this->Internal->DataTypes.clear();
for (iter->InitTraversal(); !iter->IsDoneWithTraversal(); iter->GoToNextItem())
{
vtkDataSet* ds = vtkDataSet::SafeDownCast(
iter->GetCurrentDataObject());
vtkDataSet* ds = vtkDataSet::SafeDownCast(iter->GetCurrentDataObject());
if (ds)
{
this->Internal->DataTypes.push_back(ds->GetDataObjectType());
......@@ -403,95 +432,35 @@ void vtkXMLCompositeDataWriter::CreateWriters(vtkCompositeDataSet* hdInput)
this->Internal->Writers.resize(numDatasets);
int i = 0;
for (iter->InitTraversal(); !iter->IsDoneWithTraversal(); iter->GoToNextItem())
for (iter->InitTraversal(); !iter->IsDoneWithTraversal(); iter->GoToNextItem(), ++i)
{
this->Internal->Writers[i] = NULL;
vtkDataSet* ds = vtkDataSet::SafeDownCast(
iter->GetCurrentDataObject());
if (ds)
vtkSmartPointer<vtkXMLWriter>& writer = this->Internal->Writers[i];
vtkDataSet* ds = vtkDataSet::SafeDownCast(iter->GetCurrentDataObject());
if (ds == NULL)
{
// Create a writer based on the type of this input.
switch (this->Internal->DataTypes[i])
{
case VTK_POLY_DATA:
if (!this->Internal->Writers[i].GetPointer() ||
(strcmp(this->Internal->Writers[i]->GetClassName(),
"vtkXMLPolyDataWriter") != 0))
{
vtkXMLPolyDataWriter* w = vtkXMLPolyDataWriter::New();
this->Internal->Writers[i] = w;
w->Delete();
}
vtkXMLPolyDataWriter::SafeDownCast(this->Internal->Writers[i].GetPointer())
->SetInputData(ds);
break;
case VTK_STRUCTURED_POINTS:
case VTK_IMAGE_DATA:
case VTK_UNIFORM_GRID:
if (!this->Internal->Writers[i].GetPointer() ||
(strcmp(this->Internal->Writers[i]->GetClassName(),
"vtkXMLImageDataWriter") != 0))
{
vtkXMLImageDataWriter* w = vtkXMLImageDataWriter::New();
this->Internal->Writers[i] = w;
w->Delete();
}
vtkXMLImageDataWriter::SafeDownCast(this->Internal->Writers[i].GetPointer())
->SetInputData(ds);
break;
case VTK_UNSTRUCTURED_GRID:
if (!this->Internal->Writers[i].GetPointer() ||
(strcmp(this->Internal->Writers[i]->GetClassName(),
"vtkXMLUnstructuredGridWriter") != 0))
{
vtkXMLUnstructuredGridWriter* w = vtkXMLUnstructuredGridWriter::New();
this->Internal->Writers[i] = w;
w->Delete();
}
vtkXMLUnstructuredGridWriter::SafeDownCast(
this->Internal->Writers[i].GetPointer())->SetInputData(ds);
break;
case VTK_STRUCTURED_GRID:
if (!this->Internal->Writers[i].GetPointer() ||
(strcmp(this->Internal->Writers[i]->GetClassName(),
"vtkXMLStructuredGridWriter") != 0))
{
vtkXMLStructuredGridWriter* w = vtkXMLStructuredGridWriter::New();
this->Internal->Writers[i] = w;
w->Delete();
}
vtkXMLStructuredGridWriter::SafeDownCast(
this->Internal->Writers[i].GetPointer())->SetInputData(ds);
break;
case VTK_RECTILINEAR_GRID:
if (!this->Internal->Writers[i].GetPointer() ||
(strcmp(this->Internal->Writers[i]->GetClassName(),
"vtkXMLRectilinearGridWriter") != 0))
{
vtkXMLRectilinearGridWriter* w = vtkXMLRectilinearGridWriter::New();
this->Internal->Writers[i] = w;
w->Delete();
}
vtkXMLRectilinearGridWriter::SafeDownCast(
this->Internal->Writers[i].GetPointer())->SetInputData(ds);
break;
default:
this->Internal->Writers[i] = 0;
}
writer = NULL;
continue;
}
// Create a writer based on the type of this input. We just instantiate
// vtkXMLDataSetWriter. That internally creates the write type of writer
// based on the data type.
writer.TakeReference(vtkXMLDataSetWriter::NewWriter(this->Internal->DataTypes[i]));
if (writer)
{
// Copy settings to the writer.
if (vtkXMLWriter* w = this->Internal->Writers[i].GetPointer())
{
w->SetDebug(this->GetDebug());
w->SetByteOrder(this->GetByteOrder());
w->SetCompressor(this->GetCompressor());
w->SetBlockSize(this->GetBlockSize());
w->SetDataMode(this->GetDataMode());
w->SetEncodeAppendedData(this->GetEncodeAppendedData());
w->SetHeaderType(this->GetHeaderType());
}
writer->SetDebug(this->GetDebug());
writer->SetByteOrder(this->GetByteOrder());
writer->SetCompressor(this->GetCompressor());
writer->SetBlockSize(this->GetBlockSize());
writer->SetDataMode(this->GetDataMode());
writer->SetEncodeAppendedData(this->GetEncodeAppendedData());
writer->SetHeaderType(this->GetHeaderType());
writer->SetIdType(this->GetIdType());
// Pass input.
writer->SetInputDataObject(ds);
}
i++;
}
}
......@@ -593,33 +562,9 @@ vtkStdString vtkXMLCompositeDataWriter::CreatePieceFileName(
stream << this->Internal->FilePrefix.c_str() << "/"
<< this->Internal->FilePrefix.c_str()
<< "_" << piece << ".";
switch (this->Internal->DataTypes[piece])
{
case VTK_POLY_DATA:
stream << "vtp";
break;
case VTK_STRUCTURED_POINTS:
case VTK_IMAGE_DATA:
case VTK_UNIFORM_GRID:
stream << "vti";
break;
case VTK_UNSTRUCTURED_GRID:
stream << "vtu";
break;
case VTK_STRUCTURED_GRID:
stream << "vts";
break;
case VTK_RECTILINEAR_GRID:
stream << "vtr";
break;
default:
return "";
}
const char* ext = this->GetDefaultFileExtensionForDataSet(
this->Internal->DataTypes[piece]);
stream << (ext? ext : "");
return stream.str();
}
......
......@@ -113,6 +113,11 @@ protected:
const char* GetFilePrefix();
const char* GetFilePath();
// Description:
// Returns the default extension to use for the given dataset type.
// Returns NULL if an extension cannot be determined.
const char* GetDefaultFileExtensionForDataSet(int dataset_type);
// Description:
// Write the collection file if it is requested.
// This is overridden in parallel writers to communicate the hierarchy to the
......
......@@ -14,6 +14,7 @@
=========================================================================*/
#include "vtkXMLDataSetWriter.h"
#include "vtkAlgorithmOutput.h"
#include "vtkCallbackCommand.h"
#include "vtkDataSet.h"
#include "vtkHyperOctree.h"
......@@ -22,6 +23,7 @@
#include "vtkObjectFactory.h"
#include "vtkPolyData.h"
#include "vtkRectilinearGrid.h"
#include "vtkSmartPointer.h"
#include "vtkStructuredGrid.h"
#include "vtkUnstructuredGrid.h"
#include "vtkXMLHyperOctreeWriter.h"
......@@ -30,7 +32,6 @@
#include "vtkXMLRectilinearGridWriter.h"
#include "vtkXMLStructuredGridWriter.h"
#include "vtkXMLUnstructuredGridWriter.h"
#include "vtkAlgorithmOutput.h"
vtkStandardNewMacro(vtkXMLDataSetWriter);
......@@ -62,82 +63,66 @@ vtkDataSet* vtkXMLDataSetWriter::GetInput()
}
//----------------------------------------------------------------------------
int vtkXMLDataSetWriter::WriteInternal()
vtkXMLWriter* vtkXMLDataSetWriter::NewWriter(int dataset_type)
{
vtkAlgorithmOutput* input = this->GetInputConnection(0, 0);
vtkXMLWriter* writer = 0;
// Create a writer based on the data set type.
switch (this->GetInput()->GetDataObjectType())
switch (dataset_type)
{
case VTK_UNIFORM_GRID:
case VTK_IMAGE_DATA:
case VTK_STRUCTURED_POINTS:
{
vtkXMLImageDataWriter* w = vtkXMLImageDataWriter::New();
w->SetInputConnection(input);
writer = w;
} break;
return vtkXMLImageDataWriter::New();
case VTK_STRUCTURED_GRID:
{
vtkXMLStructuredGridWriter* w = vtkXMLStructuredGridWriter::New();
w->SetInputConnection(input);
writer = w;
} break;
return vtkXMLStructuredGridWriter::New();
case VTK_RECTILINEAR_GRID:
{
vtkXMLRectilinearGridWriter* w = vtkXMLRectilinearGridWriter::New();
w->SetInputConnection(input);
writer = w;
} break;
return vtkXMLRectilinearGridWriter::New();
case VTK_UNSTRUCTURED_GRID:
{
vtkXMLUnstructuredGridWriter* w = vtkXMLUnstructuredGridWriter::New();
w->SetInputConnection(input);
writer = w;
} break;
return vtkXMLUnstructuredGridWriter::New();
case VTK_POLY_DATA:
{
vtkXMLPolyDataWriter* w = vtkXMLPolyDataWriter::New();
w->SetInputConnection(input);
writer = w;
} break;
return vtkXMLPolyDataWriter::New();
case VTK_HYPER_OCTREE:
{
vtkXMLHyperOctreeWriter* w = vtkXMLHyperOctreeWriter::New();
w->SetInputConnection(input);
writer = w;
} break;
return vtkXMLHyperOctreeWriter::New();
}
return NULL;
}
// Make sure we got a valid writer for the data set.
if (!writer)
//----------------------------------------------------------------------------
int vtkXMLDataSetWriter::WriteInternal()
{
vtkSmartPointer<vtkXMLWriter> writer;
writer.TakeReference(
// Create a writer based on the data set type.
vtkXMLDataSetWriter::NewWriter(this->GetInput()->GetDataObjectType()));
if (writer)
{
vtkErrorMacro("Cannot write dataset type: "
<< this->GetInput()->GetDataObjectType() << " which is a "
<< this->GetInput()->GetClassName());
return 0;
writer->SetInputConnection(this->GetInputConnection(0, 0));
// Copy the settings to the writer.
writer->SetDebug(this->GetDebug());
writer->SetFileName(this->GetFileName());
writer->SetByteOrder(this->GetByteOrder());
writer->SetCompressor(this->GetCompressor());
writer->SetBlockSize(this->GetBlockSize());
writer->SetDataMode(this->GetDataMode());
writer->SetEncodeAppendedData(this->GetEncodeAppendedData());
writer->SetHeaderType(this->GetHeaderType());
writer->SetIdType(this->GetIdType());
writer->AddObserver(vtkCommand::ProgressEvent, this->ProgressObserver);
// Try to write.
int result = writer->Write();
// Cleanup.
writer->RemoveObserver(this->ProgressObserver);
writer->Delete();
return result;
}
// Copy the settings to the writer.
writer->SetDebug(this->GetDebug());
writer->SetFileName(this->GetFileName());
writer->SetByteOrder(this->GetByteOrder());
writer->SetCompressor(this->GetCompressor());
writer->SetBlockSize(this->GetBlockSize());
writer->SetDataMode(this->GetDataMode());
writer->SetEncodeAppendedData(this->GetEncodeAppendedData());
writer->SetHeaderType(this->GetHeaderType());
writer->SetIdType(this->GetIdType());
writer->AddObserver(vtkCommand::ProgressEvent, this->ProgressObserver);
// Try to write.
int result = writer->Write();
// Cleanup.
writer->RemoveObserver(this->ProgressObserver);
writer->Delete();
return result;
// Make sure we got a valid writer for the data set.
vtkErrorMacro("Cannot write dataset type: "
<< this->GetInput()->GetDataObjectType() << " which is a "
<< this->GetInput()->GetClassName());
return 0;
}
//----------------------------------------------------------------------------
......
......@@ -44,6 +44,13 @@ public:
vtkDataSet* GetInput();
//ETX
// Description:
// Creates a writer for the given dataset type. May return NULL for
// unsupported/unrecognized dataset types. Returns a new instance. The caller
// is responsible of calling vtkObject::Delete() or vtkObject::UnRegister() on
// it when done.
static vtkXMLWriter* NewWriter(int dataset_type);
protected:
vtkXMLDataSetWriter();
~vtkXMLDataSetWriter();
......@@ -67,6 +74,7 @@ protected:
// The observer to report progress from the internal writer.
vtkCallbackCommand* ProgressObserver;
private:
vtkXMLDataSetWriter(const vtkXMLDataSetWriter&); // Not implemented.
void operator=(const vtkXMLDataSetWriter&); // Not implemented.
......
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