Commit 53cd3199 authored by Sylvain Thery's avatar Sylvain Thery

add loading VTK to MC::Image

parent ca42b69e
......@@ -133,6 +133,9 @@ protected:
PTRINRIMAGE mImage;
#endif
template< typename T>
void readVTKBuffer(std::ifstream& in);
public:
/**
......@@ -168,7 +171,13 @@ public:
*/
bool loadInrgz(const char* filename);
bool loadVTK(const char* filename);
/**
* @brief load VTK binary mask image
* @param filename
* @return
*/
bool loadVTKBinaryMask(const char* filename);
/**
* Constructor
* @param data pointer on voxel
......
......@@ -281,15 +281,39 @@ void Image<DataType>::saveInrMask(const char* filename)
#endif
template< typename DataType >
template< typename T>
void Image<DataType>::readVTKBuffer(std::ifstream& in)
{
int total = m_WXY * m_WZ;
if (m_Data != NULL)
delete[] m_Data;
m_Data = new DataType[total];
DataType* ptr = m_Data;
T* buffer = new T[total];
in.read(reinterpret_cast<char*>(buffer), total*sizeof(T));
T* buf = buffer;
for (int i=0; i<total; ++i)
if (*buf++ !=0)
*ptr++ = 255;
else
*ptr++ = 0;
delete[] buffer;
}
template< typename DataType >
bool Image<DataType>::loadVTK(const char* filename)
bool Image<DataType>::loadVTKBinaryMask(const char* filename)
{
std::ifstream in(filename, std::ios::binary);
if (!in)
{
CGoGNerr << "Mesh_Base::loadVox: Unable to open file " << CGoGNendl;
exit(0);
return false;
}
m_SX = 1.0 ;
......@@ -297,15 +321,23 @@ bool Image<DataType>::loadVTK(const char* filename)
m_SZ = 1.0 ;
// read encoding
// while(in.good())
for (unsigned int i=0; i< 10; ++i)
std::string keyword ;
int dtType = 0;
do
{
char line[1024] ;
in.getline(line, 1024) ;
std::cout << "LINE = "<< line << std::endl;
std::istringstream line_input(line) ;
std::string keyword ;
line_input >> keyword ;
std::cout << "keyword = "<< keyword << std::endl;
if(keyword == "VTK")
{
std::cout << "reading vtk file" << std::endl;
......@@ -314,32 +346,114 @@ bool Image<DataType>::loadVTK(const char* filename)
line_input >> m_WX ;
line_input >> m_WY ;
line_input >> m_WZ ;
std::cout << "DIM= "<< m_WX << "/"<< m_WY << "/"<< m_WZ << std::endl;
} else if(keyword == "SPACING")
{
line_input >> m_SX ;
line_input >> m_SY ;
line_input >> m_SZ ;
std::cout << "SPACING= "<< m_SX << "/"<< m_SY << "/"<< m_SZ << std::endl;
}
/*else if(keyword == "ORIGIN")
else if(keyword == "SCALARS")
{
} else if(keyword == "POINT_DATA")
std::string str;
line_input >> str;
line_input >> str;
std::cout << "SCALAR= "<< str << std::endl;
if (str == "unsigned_char")
dtType = 1 ;
else if (str == "unsigned_short")
dtType = 2 ;
else if (str == "unsigned_int")
dtType = 3 ;
else if (str == "char")
dtType = 4 ;
else if (str == "short")
dtType = 5 ;
else if (str == "int")
dtType = 6 ;
}
/*else if(keyword == "POINT_DATA")
{
} */
}
}while (keyword != "LOOKUP_TABLE");
m_WXY = m_WX * m_WY;
int total = m_WXY * m_WZ;
// int total = m_WXY * m_WZ;
m_Data = new DataType[total];
// m_Data = new DataType[total];
// DataType* ptr = m_Data;
switch(dtType)
{
case 1:
readVTKBuffer<unsigned char>(in);
// {
// unsigned char* buffer = new unsigned char[total];
// in.read(reinterpret_cast<char*>(buffer), total*sizeof(unsigned char));
// unsigned char* buf = buffer;
// for (int i=0; i<total; ++i)
// if (*buf++ !=0)
// *ptr++ = 255;
// else
// *ptr++ = 0;
// delete[] buffer;
// }
break;
in.read(reinterpret_cast<char*>(m_Data), total*sizeof(DataType));
case 2:
readVTKBuffer<unsigned short>(in);
// {
// unsigned short* buffer = new unsigned short[total];
// in.read(reinterpret_cast<char*>(buffer), total*sizeof(unsigned short));
// unsigned short* buf = buffer;
// for (int i=0; i<total; ++i)
// if (*buf++ !=0)
// *ptr++ = 255;
// else
// *ptr++ = 0;
// delete[] buffer;
// }
break;
case 3:
readVTKBuffer<unsigned int>(in);
// {
// unsigned int* buffer = new unsigned int[total];
// in.read(reinterpret_cast<char*>(buffer), total*sizeof(unsigned int));
// unsigned int* buf = buffer;
// for (int i=0; i<total; ++i)
// if (*buf++ !=0)
// *ptr++ = 255;
// else
// *ptr++ = 0;
// delete[] buffer;
// }
break;
case 4:
readVTKBuffer<char>(in);
break;
case 5:
readVTKBuffer<short>(in);
break;
case 6:
readVTKBuffer<int>(in);
break;
default:
CGoGNerr << "unknown format" << CGoGNendl;
break;
}
m_Alloc=true;
return true;
}
template< typename DataType >
bool Image<DataType>::loadIPB(const char* filename)
{
......
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