Skip to content
Snippets Groups Projects
Commit ea664931 authored by Vladimír Ulman's avatar Vladimír Ulman
Browse files

Added support for reading volumetric and surface meshes in VTK.

parent 3e5f04de
No related branches found
No related tags found
No related merge requests found
......@@ -174,7 +174,7 @@ int ActiveMesh::ImportSTL(const char *filename)
}
int ActiveMesh::ImportVTK(const char *filename)
int ActiveMesh::ImportVTK(const char *filename) //surface version
{
Pos.clear();
ID.clear();
......@@ -184,8 +184,213 @@ int ActiveMesh::ImportVTK(const char *filename)
std::ifstream file(filename);
if (!file.is_open()) return 1;
//read the "header" line
char tmp[1024];
file >> tmp >> tmp; //dangerous...
//check tmp for "vtk" or complain
if (tmp[0] != 'v'
|| tmp[1] != 't'
|| tmp[2] != 'k') { file.close(); return(2); }
//read (and skip) the rest of the header line
file.ignore(10240,'\n');
//ignore "vtk output"
file.ignore(10240,'\n');
//read "ASCII"
file >> tmp;
if (tmp[0] != 'A'
|| tmp[1] != 'S'
|| tmp[2] != 'C'
|| tmp[3] != 'I'
|| tmp[4] != 'I') { file.close(); return(3); }
file.ignore(10240,'\n');
//ignore "DATASET POLYDATA"
file.ignore(10240,'\n');
//read points header
int itemCount;
file >> tmp >> itemCount;;
if (tmp[0] != 'P'
|| tmp[1] != 'O'
|| tmp[2] != 'I'
|| tmp[3] != 'N'
|| tmp[4] != 'T'
|| tmp[5] != 'S') { file.close(); return(4); }
file.ignore(10240,'\n');
std::cout << "reading " << itemCount << " point coordinates\n";
Pos.reserve(itemCount);
//read all points...
float x,y,z;
while (itemCount > 0 && file >> x)
{
file >> y >> z;
//... and save them
Vector3FC v1(x,y,z);
v1*=100.f;
Pos.push_back(v1);
--itemCount;
}
std::cout << "last coordinate was: " << x << "," << y << "," << z << "\n";
//prepare "information about faces normals"
Vector3F fictiveNormal(1.f,0.f,0.f);
//read polyhedra header
file >> tmp >> itemCount;
if (tmp[0] != 'P'
|| tmp[1] != 'O'
|| tmp[2] != 'L'
|| tmp[3] != 'Y'
|| tmp[4] != 'G'
|| tmp[5] != 'O'
|| tmp[6] != 'N'
|| tmp[7] != 'S') { file.close(); return(5); }
file.ignore(10240,'\n');
std::cout << "reading " << itemCount << " triangles\n";
ID.reserve(3*itemCount);
norm.reserve(itemCount);
//read all polyhedra vertices
int ignore,v1,v2,v3;
while (itemCount > 0 && file >> ignore && ignore == 3)
{
file >> v1 >> v2 >> v3;
//save v1,v2,v3 (TODO: if not already saved...)
ID.push_back(v1);
ID.push_back(v2);
ID.push_back(v3);
norm.push_back(fictiveNormal);
--itemCount;
}
std::cout << "last triangle was: " << v1 << "," << v2 << "," << v3 << "\n";
file.close();
return(0);
}
int ActiveMesh::ImportVTK_Volumetric(const char *filename)
{
Pos.clear();
ID.clear();
norm.clear();
//try to open the file
std::ifstream file(filename);
if (!file.is_open()) return 1;
//read the "header" line
char tmp[1024];
file >> tmp >> tmp; //dangerous...
//check tmp for "vtk" or complain
if (tmp[0] != 'v'
|| tmp[1] != 't'
|| tmp[2] != 'k') { file.close(); return(2); }
//read (and skip) the rest of the header line
file.ignore(10240,'\n');
//ignore "vtk output"
file.ignore(10240,'\n');
//read "ASCII"
file >> tmp;
if (tmp[0] != 'A'
|| tmp[1] != 'S'
|| tmp[2] != 'C'
|| tmp[3] != 'I'
|| tmp[4] != 'I') { file.close(); return(3); }
file.ignore(10240,'\n');
//ignore "DATASET UNSTRUCTURED GRID"
file.ignore(10240,'\n');
//read points header
int itemCount;
file >> tmp >> itemCount;;
if (tmp[0] != 'P'
|| tmp[1] != 'O'
|| tmp[2] != 'I'
|| tmp[3] != 'N'
|| tmp[4] != 'T'
|| tmp[5] != 'S') { file.close(); return(4); }
file.ignore(10240,'\n');
std::cout << "reading " << itemCount << " point coordinates\n";
Pos.reserve(itemCount);
//read all points...
float x,y,z;
while (itemCount > 0 && file >> x)
{
file >> y >> z;
//... and save them
Vector3FC v1(x,y,z);
v1*=100.f;
Pos.push_back(v1);
--itemCount;
}
std::cout << "last coordinate was: " << x << "," << y << "," << z << "\n";
//prepare "information about faces normals"
Vector3F fictiveNormal(1.f,0.f,0.f);
//read polyhedra header
file >> tmp >> itemCount;
if (tmp[0] != 'C'
|| tmp[1] != 'E'
|| tmp[2] != 'L'
|| tmp[3] != 'L'
|| tmp[4] != 'S') { file.close(); return(5); }
file.ignore(10240,'\n');
std::cout << "reading " << itemCount << " polyhedra\n";
ID.reserve(3*itemCount);
norm.reserve(itemCount);
//read all polyhedra vertices
int ignore,v1,v2,v3,v4;
while (itemCount > 0 && file >> ignore)
{
file >> v1 >> v2 >> v3 >> v4;
//save v1,v2,v3 (TODO: if not already saved...)
ID.push_back(v1);
ID.push_back(v2);
ID.push_back(v3);
norm.push_back(fictiveNormal);
//save v1,v2,v4 (TODO: if not already saved...)
ID.push_back(v1);
ID.push_back(v2);
ID.push_back(v4);
norm.push_back(fictiveNormal);
//save v1,v4,v3 (TODO: if not already saved...)
ID.push_back(v1);
ID.push_back(v4);
ID.push_back(v3);
norm.push_back(fictiveNormal);
//save v4,v2,v3 (TODO: if not already saved...)
ID.push_back(v4);
ID.push_back(v2);
ID.push_back(v3);
norm.push_back(fictiveNormal);
--itemCount;
}
std::cout << "last polyhedron was: " << v1 << "," << v2 << "," << v3 << "," << v4 << "\n";
file.close();
return(0);
......
......@@ -97,6 +97,8 @@ class ActiveMesh
///returns 0 on success, otherwise non-zero
int ImportSTL(const char* filename);
int ImportVTK(const char* filename);
int ImportVTK_Volumetric(const char* filename);
void RenderMask(i3d::Image3d<i3d::GRAY16>& mask);
void RenderMaskB(i3d::Image3d<i3d::GRAY16>& mask);
......
......@@ -20,7 +20,12 @@ int main(void)
//char filename[]="../sample_input/torus_100_30.stl";
char filename[]="../sample_input/sphere.stl";
int retval=mesh.ImportSTL(filename);
char VTKfile[]="../sample_input/ID1_t001_nucleusSurf2148.vtk"; //surface
//char VTKfile[]="../sample_input/ID1_t001_nucleus2038.vtk"; //volumetric
//int retval=mesh.ImportSTL(filename);
int retval=mesh.ImportVTK(VTKfile);
//int retval=mesh.ImportVTK_Volumetric(VTKfile);
if (retval)
{
std::cout << "some error reading file: " << retval << "\n";
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment