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::VtkType [private]

Enumerator:
VTK_UNKNOWN 
VTK_ASCII 
VTK_BINARY 

Definition at line 88 of file vtkio.h.

00089                 {
00090                         VTK_UNKNOWN,
00091                         VTK_ASCII,
00092                         VTK_BINARY                      
00093                 };

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.

00096                 {
00097                         DATATYPE_UNKNOWN,
00098                         BIT,
00099                         UNSIGNED_CHAR,
00100                         CHAR,                   
00101                         UNSIGNED_SHORT,
00102                         SHORT,
00103                         UNSIGNED_INT,
00104                         INT,
00105                         UNSIGNED_LONG,
00106                         LONG,
00107                         FLOAT,
00108                         DOUBLE
00109                 };

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.

00112                 {
00113                         DATASET_UNKNOWN,
00114                         STRUCTURED_POINTS,
00115                         STRUCTURED_GRID,
00116                         RECTILINEAR_GRID,
00117                         UNSTRUCTURED_GRID,
00118                         POLYDATA
00119                 };


Constructor & Destructor Documentation

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

VtkIO::~VtkIO (  ) 

Definition at line 67 of file vtkio.cpp.

References vtk_file.

00068 {
00069         if (vtk_file) {
00070                 fclose(vtk_file);
00071                 vtk_file = 0;
00072         }
00073 }


Member Function Documentation

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().

00165 {
00166         ENTERFUNC;
00167         bool result = false;
00168         if (first_block) {
00169                 result = Util::check_file_by_magic(first_block, MAGIC);
00170         }
00171         EXITFUNC;
00172         return result;
00173 }

int VtkIO::to_em_datatype ( int  vtk_datatype  )  [private]

Definition at line 345 of file vtkio.cpp.

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

00346 {
00347         DataType d = static_cast < DataType > (vtk_datatype);
00348         switch (d) {
00349         case UNSIGNED_SHORT:
00350                 return EMUtil::EM_USHORT;
00351         case FLOAT:
00352                 return EMUtil::EM_FLOAT;
00353         default:
00354                 break;
00355         }
00356         return EMUtil::EM_UNKNOWN;
00357 }

int VtkIO::get_mode_size ( DataType  d  )  [private]

Definition at line 360 of file vtkio.cpp.

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

00361 {
00362         switch (d) {
00363         case UNSIGNED_CHAR:
00364         case CHAR:
00365                 return sizeof(char);
00366         case UNSIGNED_SHORT:
00367         case SHORT:
00368                 return sizeof(short);
00369         case UNSIGNED_INT:
00370         case INT:
00371                 return sizeof(int);
00372         case UNSIGNED_LONG:
00373         case LONG:
00374                 return sizeof(long);
00375         case FLOAT:
00376                 return sizeof(float);
00377         case DOUBLE:
00378                 return sizeof(double);
00379         default:
00380                 LOGERR("don't support this data type '%d'", d);
00381                 break;
00382         }
00383         return 0;
00384 }

void VtkIO::read_dataset ( DatasetType  dstype  )  [private]

Definition at line 440 of file vtkio.cpp.

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

00441 {
00442         char buf[1024];
00443         int bufsz = sizeof(buf);
00444 
00445         if (dstype == STRUCTURED_POINTS) {
00446                 int nlines = 3;
00447                 int i = 0;
00448                 while (i < nlines && fgets(buf, bufsz, vtk_file)) {
00449                         if (samestr(buf, "DIMENSIONS")) {
00450                                 sscanf(buf, "DIMENSIONS %d %d %d", &nx, &ny, &nz);
00451                         }
00452                         else if (samestr(buf, "ORIGIN")) {
00453                                 sscanf(buf, "ORIGIN %f %f %f", &originx, &originy, &originz);
00454                         }
00455                         else if (samestr(buf, "SPACING") || samestr(buf, "ASPECT_RATIO")) {
00456                                 if (samestr(buf, "SPACING")) {
00457                                         sscanf(buf, "SPACING %f %f %f", &spacingx, &spacingy, &spacingz);
00458                                 }
00459                                 else {
00460                                         sscanf(buf, "ASPECT_RATIO %f %f %f", &spacingx, &spacingy, &spacingz);
00461                                 }
00462 
00463                                 if (spacingx != spacingy || spacingx != spacingz || spacingy != spacingz) {
00464                                         throw ImageReadException(filename,
00465                                                                                          "not support non-uniform spacing VTK so far\n");
00466                                 }
00467                         }
00468                         i++;
00469                 }
00470 
00471                 if (i != nlines) {
00472                         throw ImageReadException(filename, "read VTK file failed");
00473                 }
00474         }
00475         else {
00476                 throw ImageReadException(filename, "only STRUCTURED_POINTS is supported so far");
00477         }
00478 }

VtkIO::DataType VtkIO::get_datatype_from_name ( const string &  datatype_name  )  [private]

Definition at line 386 of file vtkio.cpp.

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

00387 {
00388         static bool initialized = false;
00389         static map < string, VtkIO::DataType > datatypes;
00390 
00391         if (!initialized) {
00392                 datatypes["bit"] = BIT;
00393 
00394                 datatypes["unsigned_char"] = UNSIGNED_CHAR;
00395                 datatypes["char"] = CHAR;
00396 
00397                 datatypes["unsigned_short"] = UNSIGNED_SHORT;
00398                 datatypes["short"] = SHORT;
00399 
00400                 datatypes["unsigned_int"] = UNSIGNED_INT;
00401                 datatypes["int"] = INT;
00402 
00403                 datatypes["unsigned_long"] = UNSIGNED_LONG;
00404                 datatypes["long"] = LONG;
00405 
00406                 datatypes["float"] = FLOAT;
00407                 datatypes["double"] = DOUBLE;
00408                 initialized = true;
00409         }
00410 
00411         DataType result = DATATYPE_UNKNOWN;
00412 
00413         if (datatypes.find(datatype_name) != datatypes.end()) {
00414                 result = datatypes[datatype_name];
00415         }
00416         return result;
00417 }

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

Definition at line 419 of file vtkio.cpp.

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

00420 {
00421 
00422         static bool initialized = false;
00423         static map < string, DatasetType > types;
00424 
00425         if (!initialized) {
00426                 types["STRUCTURED_POINTS"] = STRUCTURED_POINTS;
00427                 types["STRUCTURED_GRID"] = STRUCTURED_GRID;
00428                 types["RECTILINEAR_GRID"] = RECTILINEAR_GRID;
00429                 types["UNSTRUCTURED_GRID"] = UNSTRUCTURED_GRID;
00430                 types["POLYDATA"] = POLYDATA;
00431         }
00432 
00433         DatasetType result = DATASET_UNKNOWN;
00434         if (types.find(dataset_name) != types.end()) {
00435                 result = types[dataset_name];
00436         }
00437         return result;
00438 }


Member Data Documentation

Definition at line 82 of file vtkio.h.

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

Definition at line 86 of file vtkio.h.

Referenced by is_valid().

string EMAN::VtkIO::filename [private]

Definition at line 128 of file vtkio.h.

Referenced by read_dataset().

Definition at line 129 of file vtkio.h.

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

Definition at line 130 of file vtkio.h.

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

Definition at line 131 of file vtkio.h.

bool EMAN::VtkIO::is_new_file [private]

Definition at line 132 of file vtkio.h.

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 135 of file vtkio.h.

Definition at line 136 of file vtkio.h.

int EMAN::VtkIO::nx [private]

Definition at line 137 of file vtkio.h.

Referenced by read_dataset().

int EMAN::VtkIO::ny [private]

Definition at line 138 of file vtkio.h.

int EMAN::VtkIO::nz [private]

Definition at line 139 of file vtkio.h.

float EMAN::VtkIO::originx [private]

Definition at line 140 of file vtkio.h.

Referenced by read_dataset().

float EMAN::VtkIO::originy [private]

Definition at line 141 of file vtkio.h.

Referenced by read_dataset().

float EMAN::VtkIO::originz [private]

Definition at line 142 of file vtkio.h.

Referenced by read_dataset().

float EMAN::VtkIO::spacingx [private]

Definition at line 143 of file vtkio.h.

Referenced by read_dataset().

float EMAN::VtkIO::spacingy [private]

Definition at line 144 of file vtkio.h.

Referenced by read_dataset().

float EMAN::VtkIO::spacingz [private]

Definition at line 145 of file vtkio.h.

Referenced by read_dataset().

off_t EMAN::VtkIO::file_offset [private]

Definition at line 146 of file vtkio.h.


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

Generated on Sat Nov 21 02:20:50 2009 for EMAN2 by  doxygen 1.5.6