EMAN2
Public Member Functions | Static Public Member Functions | Public Attributes | Private Types | Private Member Functions | Private Attributes | Static Private Attributes
EMAN::VtkIO Class Reference

VtkIO reads/writes VTK image file. More...

#include <vtkio.h>

Inheritance diagram for EMAN::VtkIO:
Inheritance graph
[legend]
Collaboration diagram for EMAN::VtkIO:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 VtkIO (const string &filename, IOMode rw_mode=READ_ONLY)
 ~VtkIO ()

Static Public Member Functions

static bool is_valid (const void *first_block)

Public Attributes

 DEFINE_IMAGEIO_FUNC

Private Types

enum  VtkType { VTK_UNKNOWN, VTK_ASCII, VTK_BINARY }
enum  DataType {
  DATATYPE_UNKNOWN, BIT, UNSIGNED_CHAR, CHAR,
  UNSIGNED_SHORT, SHORT, UNSIGNED_INT, INT,
  UNSIGNED_LONG, LONG, FLOAT, DOUBLE
}
enum  DatasetType {
  DATASET_UNKNOWN, STRUCTURED_POINTS, STRUCTURED_GRID, RECTILINEAR_GRID,
  UNSTRUCTURED_GRID, POLYDATA
}

Private Member Functions

int to_em_datatype (int vtk_datatype)
int get_mode_size (DataType d)
void read_dataset (DatasetType dstype)
DataType get_datatype_from_name (const string &datatype_name)
DatasetType get_datasettype_from_name (const string &dataset_name)

Private Attributes

string filename
IOMode rw_mode
FILE * vtk_file
bool is_big_endian
bool is_new_file
bool initialized
DataType datatype
VtkType filetype
int nx
int ny
int nz
float originx
float originy
float originz
float spacingx
float spacingy
float spacingz
off_t file_offset

Static Private Attributes

static const char * MAGIC = "# vtk DataFile Version"

Detailed Description

VtkIO reads/writes VTK image file.

VTK is a file format used by the Visual Toolkit. (http://public.kitware.com/VTK/)

There are 2 VTK formats: ASCII or Binary.

ASCII format It has 5 parts:

Binary format It has the same 5 parts like ASCII format,followed by data in binary format. The data are stored in big endian by default..

A VTK file contains 1 2D or 3D image.

Definition at line 76 of file vtkio.h.


Member Enumeration Documentation

enum EMAN::VtkIO::DatasetType [private]
Enumerator:
DATASET_UNKNOWN 
STRUCTURED_POINTS 
STRUCTURED_GRID 
RECTILINEAR_GRID 
UNSTRUCTURED_GRID 
POLYDATA 

Definition at line 111 of file vtkio.h.

enum EMAN::VtkIO::DataType [private]
Enumerator:
DATATYPE_UNKNOWN 
BIT 
UNSIGNED_CHAR 
CHAR 
UNSIGNED_SHORT 
SHORT 
UNSIGNED_INT 
INT 
UNSIGNED_LONG 
LONG 
FLOAT 
DOUBLE 

Definition at line 95 of file vtkio.h.

enum EMAN::VtkIO::VtkType [private]
Enumerator:
VTK_UNKNOWN 
VTK_ASCII 
VTK_BINARY 

Definition at line 88 of file vtkio.h.


Constructor & Destructor Documentation

VtkIO::VtkIO ( const string &  filename,
IOMode  rw_mode = READ_ONLY 
) [explicit]
VtkIO::~VtkIO ( )

Definition at line 67 of file vtkio.cpp.

References vtk_file.

{
        if (vtk_file) {
                fclose(vtk_file);
                vtk_file = 0;
        }
}

Member Function Documentation

VtkIO::DatasetType VtkIO::get_datasettype_from_name ( const string &  dataset_name) [private]

Definition at line 431 of file vtkio.cpp.

References DATASET_UNKNOWN, initialized, POLYDATA, RECTILINEAR_GRID, STRUCTURED_GRID, STRUCTURED_POINTS, and UNSTRUCTURED_GRID.

{

        static bool initialized = false;
        static map < string, DatasetType > types;

        if (!initialized) {
                types["STRUCTURED_POINTS"] = STRUCTURED_POINTS;
                types["STRUCTURED_GRID"] = STRUCTURED_GRID;
                types["RECTILINEAR_GRID"] = RECTILINEAR_GRID;
                types["UNSTRUCTURED_GRID"] = UNSTRUCTURED_GRID;
                types["POLYDATA"] = POLYDATA;
        }

        DatasetType result = DATASET_UNKNOWN;
        if (types.find(dataset_name) != types.end()) {
                result = types[dataset_name];
        }
        return result;
}
VtkIO::DataType VtkIO::get_datatype_from_name ( const string &  datatype_name) [private]

Definition at line 398 of file vtkio.cpp.

References BIT, CHAR, DATATYPE_UNKNOWN, DOUBLE, FLOAT, initialized, INT, LONG, SHORT, UNSIGNED_CHAR, UNSIGNED_INT, UNSIGNED_LONG, and UNSIGNED_SHORT.

{
        static bool initialized = false;
        static map < string, VtkIO::DataType > datatypes;

        if (!initialized) {
                datatypes["bit"] = BIT;

                datatypes["unsigned_char"] = UNSIGNED_CHAR;
                datatypes["char"] = CHAR;

                datatypes["unsigned_short"] = UNSIGNED_SHORT;
                datatypes["short"] = SHORT;

                datatypes["unsigned_int"] = UNSIGNED_INT;
                datatypes["int"] = INT;

                datatypes["unsigned_long"] = UNSIGNED_LONG;
                datatypes["long"] = LONG;

                datatypes["float"] = FLOAT;
                datatypes["double"] = DOUBLE;
                initialized = true;
        }

        DataType result = DATATYPE_UNKNOWN;

        if (datatypes.find(datatype_name) != datatypes.end()) {
                result = datatypes[datatype_name];
        }
        return result;
}
int VtkIO::get_mode_size ( DataType  d) [private]

Definition at line 372 of file vtkio.cpp.

References CHAR, DOUBLE, FLOAT, INT, LOGERR, LONG, SHORT, UNSIGNED_CHAR, UNSIGNED_INT, UNSIGNED_LONG, and UNSIGNED_SHORT.

{
        switch (d) {
        case UNSIGNED_CHAR:
        case CHAR:
                return sizeof(char);
        case UNSIGNED_SHORT:
        case SHORT:
                return sizeof(short);
        case UNSIGNED_INT:
        case INT:
                return sizeof(int);
        case UNSIGNED_LONG:
        case LONG:
                return sizeof(long);
        case FLOAT:
                return sizeof(float);
        case DOUBLE:
                return sizeof(double);
        default:
                LOGERR("don't support this data type '%d'", d);
                break;
        }
        return 0;
}
bool VtkIO::is_valid ( const void *  first_block) [static]

Definition at line 164 of file vtkio.cpp.

References EMAN::Util::check_file_by_magic(), ENTERFUNC, EXITFUNC, and MAGIC.

Referenced by EMAN::EMUtil::fast_get_image_type(), and EMAN::EMUtil::get_image_type().

{
        ENTERFUNC;
        bool result = false;
        if (first_block) {
                result = Util::check_file_by_magic(first_block, MAGIC);
        }
        EXITFUNC;
        return result;
}
void VtkIO::read_dataset ( DatasetType  dstype) [private]

Definition at line 452 of file vtkio.cpp.

References filename, ImageReadException, nx, originx, originy, originz, samestr(), spacingx, spacingy, spacingz, STRUCTURED_POINTS, and vtk_file.

{
        char buf[1024];
        int bufsz = sizeof(buf);

        if (dstype == STRUCTURED_POINTS) {
                int nlines = 3;
                int i = 0;
                while (i < nlines && fgets(buf, bufsz, vtk_file)) {
                        if (samestr(buf, "DIMENSIONS")) {
                                sscanf(buf, "DIMENSIONS %d %d %d", &nx, &ny, &nz);
                        }
                        else if (samestr(buf, "ORIGIN")) {
                                sscanf(buf, "ORIGIN %f %f %f", &originx, &originy, &originz);
                        }
                        else if (samestr(buf, "SPACING") || samestr(buf, "ASPECT_RATIO")) {
                                if (samestr(buf, "SPACING")) {
                                        sscanf(buf, "SPACING %f %f %f", &spacingx, &spacingy, &spacingz);
                                }
                                else {
                                        sscanf(buf, "ASPECT_RATIO %f %f %f", &spacingx, &spacingy, &spacingz);
                                }

                                if (spacingx != spacingy || spacingx != spacingz || spacingy != spacingz) {
                                        throw ImageReadException(filename,
                                                                                         "not support non-uniform spacing VTK so far\n");
                                }
                        }
                        i++;
                }

                if (i != nlines) {
                        throw ImageReadException(filename, "read VTK file failed");
                }
        }
        else {
                throw ImageReadException(filename, "only STRUCTURED_POINTS is supported so far");
        }
}
int VtkIO::to_em_datatype ( int  vtk_datatype) [private]

Definition at line 357 of file vtkio.cpp.

References EMAN::EMUtil::EM_FLOAT, EMAN::EMUtil::EM_UNKNOWN, EMAN::EMUtil::EM_USHORT, FLOAT, and UNSIGNED_SHORT.

{
        DataType d = static_cast < DataType > (vtk_datatype);
        switch (d) {
        case UNSIGNED_SHORT:
                return EMUtil::EM_USHORT;
        case FLOAT:
                return EMUtil::EM_FLOAT;
        default:
                break;
        }
        return EMUtil::EM_UNKNOWN;
}

Member Data Documentation

Definition at line 135 of file vtkio.h.

Referenced by VtkIO().

Definition at line 82 of file vtkio.h.

off_t EMAN::VtkIO::file_offset [private]

Definition at line 146 of file vtkio.h.

Referenced by VtkIO().

string EMAN::VtkIO::filename [private]

Definition at line 128 of file vtkio.h.

Referenced by read_dataset().

Definition at line 136 of file vtkio.h.

Referenced by VtkIO().

bool EMAN::VtkIO::initialized [private]

Definition at line 133 of file vtkio.h.

Referenced by get_datasettype_from_name(), and get_datatype_from_name().

Definition at line 131 of file vtkio.h.

Referenced by VtkIO().

bool EMAN::VtkIO::is_new_file [private]

Definition at line 132 of file vtkio.h.

Referenced by VtkIO().

const char * VtkIO::MAGIC = "# vtk DataFile Version" [static, private]

Definition at line 86 of file vtkio.h.

Referenced by is_valid().

int EMAN::VtkIO::nx [private]

Definition at line 137 of file vtkio.h.

Referenced by read_dataset(), and VtkIO().

int EMAN::VtkIO::ny [private]

Definition at line 138 of file vtkio.h.

Referenced by VtkIO().

int EMAN::VtkIO::nz [private]

Definition at line 139 of file vtkio.h.

Referenced by VtkIO().

float EMAN::VtkIO::originx [private]

Definition at line 140 of file vtkio.h.

Referenced by read_dataset(), and VtkIO().

float EMAN::VtkIO::originy [private]

Definition at line 141 of file vtkio.h.

Referenced by read_dataset(), and VtkIO().

float EMAN::VtkIO::originz [private]

Definition at line 142 of file vtkio.h.

Referenced by read_dataset(), and VtkIO().

Definition at line 129 of file vtkio.h.

float EMAN::VtkIO::spacingx [private]

Definition at line 143 of file vtkio.h.

Referenced by read_dataset(), and VtkIO().

float EMAN::VtkIO::spacingy [private]

Definition at line 144 of file vtkio.h.

Referenced by read_dataset(), and VtkIO().

float EMAN::VtkIO::spacingz [private]

Definition at line 145 of file vtkio.h.

Referenced by read_dataset(), and VtkIO().

FILE* EMAN::VtkIO::vtk_file [private]

Definition at line 130 of file vtkio.h.

Referenced by read_dataset(), and ~VtkIO().


The documentation for this class was generated from the following files: