Commit 93b51a60 authored by Chris Harris's avatar Chris Harris
Browse files

Merge branch 'nrrdreader-refactor' into nrrdreader

Change-Id: I98db90cedcf5df5db59d32e25951b044959e5db7
parents 149cf5a4 97f27154
......@@ -9,11 +9,21 @@ option(VTK_USE_64BIT_IDS "Build VTK with 64 bit ids"
${VTK_USE_64BIT_IDS_DEFAULT})
option(VTK_DEBUG_LEAKS "Build leak checking support into VTK." OFF)
mark_as_advanced(VTK_DEBUG_LEAKS VTK_USE_64BIT_IDS)
# This option determines the behavior of the New methods of vtkObject derived
# classes. If this option is off then only classes specifically using the
# vtkObjectFactoryNewMacro and vtkAbstractObjectFactoryNewMacro will allow for
# overrides. If the method is on then vtkStandardNewMacro will use the
# vtkObjectFactoryNewMacro's implementation body.
option(VTK_ALL_NEW_OBJECT_FACTORY
"Build all vtkObject derived classes with object factory new methods." OFF)
mark_as_advanced(VTK_DEBUG_LEAKS VTK_USE_64BIT_IDS VTK_ALL_NEW_OBJECT_FACTORY)
set(vtkCommonCore_EXPORT_OPTIONS
VTK_DEBUG_LEAKS
VTK_USE_64BIT_IDS
VTK_ALL_NEW_OBJECT_FACTORY
)
#-----------------------------------------------------------------------------
......
......@@ -148,6 +148,12 @@
#cmakedefine VTK_LEGACY_REMOVE
#cmakedefine VTK_LEGACY_SILENT
/* Debug leaks support. */
#cmakedefine VTK_DEBUG_LEAKS
/* Should all New methods use the object factory override. */
#cmakedefine VTK_ALL_NEW_OBJECT_FACTORY
/*--------------------------------------------------------------------------*/
/* Setup VTK based on platform features and configuration. */
......
......@@ -75,8 +75,16 @@ vtkObject* vtkObjectFactory::CreateInstance(const char* vtkclassname)
return 0;
}
#ifdef VTK_DEBUG_LEAKS
void vtkObjectFactory::ConstructInstance(const char* vtkclassname)
{
vtkDebugLeaks::ConstructClass(vtkclassname);
}
#else
void vtkObjectFactory::ConstructInstance(const char*)
{
}
#endif
// A one time initialization method.
void vtkObjectFactory::Init()
......
......@@ -32,14 +32,9 @@
// with the vtkObjectFactory::RegisterFactory method.
//
#ifndef __vtkObjectFactory_h
#define __vtkObjectFactory_h
#include "vtkCommonCoreModule.h" // For export macro
#include "vtkObject.h"
......@@ -59,6 +54,11 @@ public:
// first factory returns the object no other factories are asked.
static vtkObject* CreateInstance(const char* vtkclassname);
// Description:
// Call vtkDebugLeaks::ConstructClass if necessary. Does not attempt
// to use the object factory to create an instance.
static void ConstructInstance(const char* vtkclassname);
// Description:
// Create all possible instances of the named vtk object.
// Each registered vtkObjectFactory will be asked, and the
......
......@@ -640,22 +640,68 @@ virtual double *Get##name() \
# define vtkCxxRevisionMacro(thisClass, revision)
#endif
// Macro to implement the body of the object factory form of the New() method.
#define VTK_OBJECT_FACTORY_NEW_BODY(thisClass) \
vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass); \
if(ret) \
{ \
return static_cast<thisClass*>(ret); \
} \
return new thisClass;
// Macro to implement the body of the abstract object factory form of the New()
// method, i.e. an abstract base class that can only be instantiated if the
// object factory overrides it.
#define VTK_ABSTRACT_OBJECT_FACTORY_NEW_BODY(thisClass) \
vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass); \
if(ret) \
{ \
return static_cast<thisClass*>(ret); \
} \
return NULL;
// Macro to implement the body of the standard form of the New() method.
#if defined(VTK_ALL_NEW_OBJECT_FACTORY)
# define VTK_STANDARD_NEW_BODY(thisClass) \
VTK_OBJECT_FACTORY_NEW_BODY(thisClass)
#elif defined(VTK_DEBUG_LEAKS)
# define VTK_STANDARD_NEW_BODY(thisClass) \
vtkObjectFactory::ConstructInstance(#thisClass); \
return new thisClass;
#else
# define VTK_STANDARD_NEW_BODY(thisClass) \
return new thisClass;
#endif
// Macro to implement the standard form of the New() method.
#define vtkStandardNewMacro(thisClass) \
thisClass* thisClass::New() \
{ \
vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass); \
if(ret) \
{ \
return static_cast<thisClass*>(ret); \
} \
return new thisClass; \
VTK_STANDARD_NEW_BODY(thisClass) \
} \
vtkInstantiatorNewMacro(thisClass)
// Macro to implement the object factory form of the New() method.
#define vtkObjectFactoryNewMacro(thisClass) \
thisClass* thisClass::New() \
{ \
VTK_OBJECT_FACTORY_NEW_BODY(thisClass) \
} \
vtkInstantiatorNewMacro(thisClass)
// Macro to implement the abstract object factory form of the New() method.
// That is an abstract base class that can only be instantiated of the
// object factory overrides it.
#define vtkAbstractObjectFactoryNewMacro(thisClass) \
thisClass* thisClass::New() \
{ \
VTK_ABSTRACT_OBJECT_FACTORY_NEW_BODY(thisClass) \
} \
vtkInstantiatorNewMacro(thisClass)
// Macro to implement the instantiator's wrapper around the New()
// method. Use this macro if and only if vtkStandardNewMacro is not
// used by the class.
// method. Use this macro if and only if vtkStandardNewMacro or
// vtkObjectFactoryNewMacro is not used by the class.
#define vtkInstantiatorNewMacro(thisClass) \
extern vtkObject* vtkInstantiator##thisClass##New(); \
vtkObject* vtkInstantiator##thisClass##New() \
......
......@@ -28,9 +28,6 @@
/*--------------------------------------------------------------------------*/
/* Other Configuration Options */
/* Debug leaks support. */
#cmakedefine VTK_DEBUG_LEAKS
/* Whether VTK is using vfw32 and if it supports video capture */
#cmakedefine VTK_USE_VIDEO_FOR_WINDOWS
#cmakedefine VTK_VFW_SUPPORTS_CAPTURE
......
......@@ -32,6 +32,7 @@ set(Module_SRCS
vtkTIFFWriter.cxx
vtkVolume16Reader.cxx
vtkVolumeReader.cxx
vtkNrrdReader.cxx
)
if(WIN32)
......
......@@ -2,7 +2,7 @@
/*=========================================================================
Program: Visualization Toolkit
Module: vtkPNrrdReader.cxx
Module: vtkNrrdReader.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
......@@ -18,10 +18,9 @@
See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.
----------------------------------------------------------------------------*/
#include "vtkPNrrdReader.h"
#include "vtkNrrdReader.h"
#include "vtkCharArray.h"
#include "vtkDummyController.h"
#include "vtkImageData.h"
#include "vtkObjectFactory.h"
#include "vtkStringArray.h"
......@@ -37,6 +36,7 @@
#include <ctype.h>
#include "vtkSmartPointer.h"
#define VTK_CREATE(type, name) \
vtkSmartPointer<type> name = vtkSmartPointer<type>::New()
......@@ -201,28 +201,33 @@ static int NrrdType2VTKType(std::string nrrdType)
}
}
//=============================================================================
vtkStandardNewMacro(vtkPNrrdReader);
//-----------------------------------------------------------------------------
vtkObjectFactoryNewMacro(vtkNrrdReader);
//-----------------------------------------------------------------------------
vtkPNrrdReader::vtkPNrrdReader()
vtkNrrdReader::vtkNrrdReader()
{
this->DataFiles = vtkStringArray::New();
}
vtkPNrrdReader::~vtkPNrrdReader()
vtkNrrdReader::~vtkNrrdReader()
{
this->DataFiles->Delete();
this->DataFiles = NULL;
}
void vtkPNrrdReader::PrintSelf(ostream &os, vtkIndent indent)
void vtkNrrdReader::PrintSelf(ostream &os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
//-----------------------------------------------------------------------------
int vtkPNrrdReader::CanReadFile(const char *filename)
int vtkNrrdReader::CanReadFile(const char *filename)
{
ifstream file(filename, ios::in | ios::binary);
std::string firstLine;
......@@ -238,9 +243,9 @@ int vtkPNrrdReader::CanReadFile(const char *filename)
}
//-----------------------------------------------------------------------------
int vtkPNrrdReader::RequestInformation(vtkInformation *request,
vtkInformationVector **inputVector,
vtkInformationVector *outputVector)
int vtkNrrdReader::RequestInformation(vtkInformation *request,
vtkInformationVector **inputVector,
vtkInformationVector *outputVector)
{
if (!this->ReadHeader()) return 0;
......@@ -249,7 +254,7 @@ int vtkPNrrdReader::RequestInformation(vtkInformation *request,
}
//-----------------------------------------------------------------------------
int vtkPNrrdReader::ReadHeader()
int vtkNrrdReader::ReadHeaderInternal(vtkCharArray* headerBuffer)
{
if (!this->FileName)
{
......@@ -257,55 +262,58 @@ int vtkPNrrdReader::ReadHeader()
return 0;
}
VTK_CREATE(vtkCharArray, headerBuffer);
// Having a dummy controller means less cases later.
if (!this->Controller) this->Controller = vtkDummyController::New();
// Read the header on process 0 and broadcast to everyone else.
if (this->Controller->GetLocalProcessId() == 0)
{
ifstream file(this->FileName, ios::in | ios::binary);
// Read in 4 MB. Assuming that the header will be smaller than that.
headerBuffer->SetNumberOfTuples(0x400000);
file.read(headerBuffer->GetPointer(0), 0x400000-1);
vtkIdType buffersize = file.gcount();
headerBuffer->SetValue(buffersize, '\0');
headerBuffer->SetNumberOfTuples(buffersize+1);
// Find a blank line (which signals the end of the header). Be careful
// because line endings can be "\n", "\r\n", or both. It is possible that
// the entire file is the header (happens with "detached headers").
char *bufferStart = headerBuffer->GetPointer(0);
char *s = bufferStart;
while ((s = strchr(s+1, '\n')) != NULL)
ifstream file(this->FileName, ios::in | ios::binary);
// Read in 4 MB. Assuming that the header will be smaller than that.
headerBuffer->SetNumberOfTuples(0x400000);
file.read(headerBuffer->GetPointer(0), 0x400000-1);
vtkIdType buffersize = file.gcount();
headerBuffer->SetValue(buffersize, '\0');
headerBuffer->SetNumberOfTuples(buffersize+1);
// Find a blank line (which signals the end of the header). Be careful
// because line endings can be "\n", "\r\n", or both. It is possible that
// the entire file is the header (happens with "detached headers").
char *bufferStart = headerBuffer->GetPointer(0);
char *s = bufferStart;
while ((s = strchr(s+1, '\n')) != NULL)
{
// If you find a double line ending, terminate the string and shorten the
// buffer.
if (s[1] == '\n')
{
// If you find a double line ending, terminate the string and shorten the
// buffer.
if (s[1] == '\n')
{
s[2] = '\0';
headerBuffer->SetNumberOfTuples(
static_cast<vtkIdType>(s + 3 - bufferStart));
break;
}
if ((s[1] == '\r') && (s[2] == '\n'))
{
s[3] = '\0';
headerBuffer->SetNumberOfTuples(
static_cast<vtkIdType>(s + 4 - bufferStart));
break;
}
s[2] = '\0';
headerBuffer->SetNumberOfTuples(
static_cast<vtkIdType>(s + 3 - bufferStart));
break;
}
if ((s[1] == '\r') && (s[2] == '\n'))
{
s[3] = '\0';
headerBuffer->SetNumberOfTuples(
static_cast<vtkIdType>(s + 4 - bufferStart));
break;
}
}
this->Controller->Broadcast(headerBuffer, 0);
return 1;
}
//-----------------------------------------------------------------------------
int vtkNrrdReader::ReadHeader()
{
VTK_CREATE(vtkCharArray, headerBuffer);
if(!this->ReadHeaderInternal(headerBuffer))
{
return 0;
}
return this->ReadHeader(headerBuffer);
}
//-----------------------------------------------------------------------------
int vtkPNrrdReader::ReadHeader(vtkCharArray *headerBuffer)
int vtkNrrdReader::ReadHeader(vtkCharArray *headerBuffer)
{
this->HeaderSize = headerBuffer->GetNumberOfTuples();
......@@ -609,9 +617,9 @@ int vtkPNrrdReader::ReadHeader(vtkCharArray *headerBuffer)
}
//=============================================================================
int vtkPNrrdReader::RequestData(vtkInformation *request,
vtkInformationVector **inputVector,
vtkInformationVector *outputVector)
int vtkNrrdReader::RequestData(vtkInformation *request,
vtkInformationVector **inputVector,
vtkInformationVector *outputVector)
{
// Get rid of superclasses FileNames. We don't support that functionality,
// but we exploit that of the superclass.
......
......@@ -2,7 +2,7 @@
/*=========================================================================
Program: Visualization Toolkit
Module: vtkPNrrdReader.h
Module: vtkNrrdReader.h
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
......@@ -18,14 +18,10 @@
See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.
----------------------------------------------------------------------------*/
// .NAME vtkPNrrdReader - Read nrrd files efficiently from parallel file systems (and reasonably well elsewhere).
// .NAME vtkNrrdReader - Read nrrd files file system
//
// .SECTION Description
//
// vtkPNrrdReader is a subclass of vtkMPIImageReader that will read Nrrd format
// header information of the image before reading the data. This means that the
// reader will automatically set information like file dimensions.
//
// .SECTION Bugs
//
// There are several limitations on what type of nrrd files we can read. This
......@@ -34,26 +30,26 @@
// supports reading one file that is detached.
//
#ifndef __vtkPNrrdReader_h
#define __vtkPNrrdReader_h
#ifndef __vtkNrrdReader_h
#define __vtkNrrdReader_h
#include "vtkIOParallelMPIModule.h" // For export macro
#include "vtkMPIImageReader.h"
#include "vtkIOImageModule.h" // For export macro
#include "vtkImageReader.h"
class vtkCharArray;
class VTKIOPARALLELMPI_EXPORT vtkPNrrdReader : public vtkMPIImageReader
class VTKIOIMAGE_EXPORT vtkNrrdReader : public vtkImageReader
{
public:
vtkTypeMacro(vtkPNrrdReader, vtkMPIImageReader);
static vtkPNrrdReader *New();
vtkTypeMacro(vtkNrrdReader, vtkImageReader);
static vtkNrrdReader *New();
virtual void PrintSelf(ostream &os, vtkIndent indent);
virtual int CanReadFile(const char *filename);
protected:
vtkPNrrdReader();
~vtkPNrrdReader();
vtkNrrdReader();
~vtkNrrdReader();
virtual int RequestInformation(vtkInformation *request,
vtkInformationVector **inputVector,
......@@ -63,14 +59,15 @@ protected:
vtkInformationVector **inputVector,
vtkInformationVector *outputVector);
int ReadHeaderInternal(vtkCharArray *headerBuffer);
virtual int ReadHeader();
virtual int ReadHeader(vtkCharArray *headerBuffer);
vtkStringArray *DataFiles;
private:
vtkPNrrdReader(const vtkPNrrdReader &); // Not implemented.
void operator=(const vtkPNrrdReader &); // Not implemented.
vtkNrrdReader(const vtkNrrdReader &); // Not implemented.
void operator=(const vtkNrrdReader &); // Not implemented.
};
#endif //__vtkPNrrdReader_h
#endif //__vtkNrrdReader_h
find_package(MPI REQUIRED)
include_directories(${MPI_INCLUDE_PATH})
set(Module_SRCS
vtkPNrrdReader.cxx
${CMAKE_CURRENT_BINARY_DIR}/${vtk-module}ObjectFactory.cxx
)
set_source_files_properties(
${vtk-module}ObjectFactory
WRAP_EXCLUDE
)
# Now to generate our object factory.
set( vtk_module_overrides
NrrdReader
)
foreach(_class ${vtk_module_overrides})
set(_override vtkP${_class})
set(_vtk_override_includes "${_vtk_override_includes} #include \"${_override}.h\"")
set(_vtk_override_creates "${_vtk_override_creates}
VTK_CREATE_CREATE_FUNCTION(${_override})")
set(_vtk_override_do "${_vtk_override_do}
this->RegisterOverride(\"${_class}\",
\"${_override}\",
\"Override for ${vtk-module} module\", 1,
vtkObjectFactoryCreate${_override});")
endforeach()
# Now lets create the object factory classes
string(TOUPPER ${vtk-module} VTK-MODULE)
configure_file(${VTK_CMAKE_DIR}/vtkObjectFactory.h.in
${CMAKE_CURRENT_BINARY_DIR}/${vtk-module}ObjectFactory.h)
configure_file(${VTK_CMAKE_DIR}/vtkObjectFactory.cxx.in
${CMAKE_CURRENT_BINARY_DIR}/${vtk-module}ObjectFactory.cxx)
vtk_module_library(vtkIOMPIImage ${Module_SRCS})
vtk_module(vtkIOParallelMPI
vtk_module(vtkIOMPIImage
IMPLEMENTS
vtkIOImage
GROUPS
MPI
DEPENDS
vtkParallelCore
vtkParallelMPI
vtkIOImage
)
......@@ -2,7 +2,7 @@
/*=========================================================================
Program: Visualization Toolkit
Module: vtkMPIImageReader.cxx
Module: vtkPNrrdReader.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
......@@ -18,17 +18,30 @@
See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.
----------------------------------------------------------------------------*/
#include "vtkMPIImageReader.h"
#include "vtkPNrrdReader.h"
#include "vtkMultiProcessController.h"
#include "vtkCharArray.h"
#include "vtkDummyController.h"
#include "vtkImageData.h"
#include "vtkObjectFactory.h"
#include "vtkToolkits.h"
#ifdef VTK_USE_MPI
// Include the MPI headers and then determine if MPIIO is available.
#include "vtkStringArray.h"
#include <string>
#include <vector>
#include <vtksys/SystemTools.hxx>
#include <istream>
#include <sstream>
#include <math.h>
#include <string.h>
#include <ctype.h>
#include "vtkSmartPointer.h"
#include "vtkMPI.h"
#define VTK_CREATE(type, name) \
vtkSmartPointer<type> name = vtkSmartPointer<type>::New()
// Determine if MPIIO is available.
#ifdef MPI_VERSION
# if (MPI_VERSION >= 2)
# define VTK_USE_MPI_IO 1
......@@ -41,8 +54,6 @@
# define VTK_USE_MPI_IO 1
#endif
#endif // VTK_USE_MPI
// If VTK_USE_MPI_IO is set, that means we will read the data ourself using
// MPIIO. Otherwise, just delegate everything to the superclass.
......@@ -73,20 +84,19 @@
<< errormsg); \
} \
}
#endif // VTK_USE_MPI_IO
//=============================================================================
vtkStandardNewMacro(vtkMPIImageReader);
//-----------------------------------------------------------------------------
vtkCxxSetObjectMacro(vtkMPIImageReader, Controller, vtkMultiProcessController);
vtkCxxSetObjectMacro(vtkMPIImageReader, GroupedController,
vtkStandardNewMacro(vtkPNrrdReader);
vtkCxxSetObjectMacro(vtkPNrrdReader, Controller, vtkMultiProcessController);
vtkCxxSetObjectMacro(vtkPNrrdReader, GroupedController,
vtkMultiProcessController);
//-----------------------------------------------------------------------------
#ifdef VTK_USE_MPI_IO
template<class T>
inline void vtkMPIImageReaderMaskBits(T *data, vtkIdType length,
inline void vtkPNrrdReaderMaskBits(T *data, vtkIdType length,
vtkTypeUInt64 _mask)
{
T mask = (T)_mask;
......@@ -102,12 +112,12 @@ inline void vtkMPIImageReaderMaskBits(T *data, vtkIdType length,
// Override float and double because masking bits for them makes no sense.
VTK_TEMPLATE_SPECIALIZE
void vtkMPIImageReaderMaskBits(float *, vtkIdType, vtkTypeUInt64)
void vtkPNrrdReaderMaskBits(float *, vtkIdType, vtkTypeUInt64)
{
return;
}
VTK_TEMPLATE_SPECIALIZE
void vtkMPIImageReaderMaskBits(double *, vtkIdType, vtkTypeUInt64)
void vtkPNrrdReaderMaskBits(double *, vtkIdType, vtkTypeUInt64)
{
return;
}
......@@ -124,30 +134,28 @@ namespace {
};
#endif //VTK_USE_MPI_IO
//=============================================================================
vtkMPIImageReader::vtkMPIImageReader()
//-----------------------------------------------------------------------------
vtkPNrrdReader::vtkPNrrdReader()
{
this->Controller = NULL;
this->SetController(vtkMultiProcessController::GetGlobalController());
this->GroupedController = NULL;
}
vtkMPIImageReader::~vtkMPIImageReader()
vtkPNrrdReader::~vtkPNrrdReader()
{
this->SetController(NULL);
this->SetGroupedController(NULL);
}
void vtkMPIImageReader::PrintSelf(ostream &os, vtkIndent indent)
void vtkPNrrdReader::PrintSelf(ostream &os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);