Commit 8accb733 authored by Alexis Girault's avatar Alexis Girault
Browse files

ENH: Finish VTKMeshReader

reads polydata and unstructured grid meshes
parent f33f9825
......@@ -24,10 +24,16 @@
namespace imstk {
void
HexahedralMesh::initialize(const std::vector<Vec3d>& vertices,
const std::vector<HexaArray>& hexahedra)
const std::vector<HexaArray>& hexahedra,
bool computeAttachedSurfaceMesh)
{
Mesh::initialize(vertices);
this->setHexahedraVertices(hexahedra);
if(computeAttachedSurfaceMesh)
{
this->computeAttachedSurfaceMesh();
}
}
void
......
......@@ -48,7 +48,8 @@ public:
/// hexahedra connectivity
///
void initialize(const std::vector<Vec3d>& vertices,
const std::vector<HexaArray>& hexahedra);
const std::vector<HexaArray>& hexahedra,
bool computeAttachedSurfaceMesh = false);
///
/// \brief Clear all the mesh data
......
......@@ -24,10 +24,16 @@
namespace imstk {
void
TetrahedralMesh::initialize(const std::vector<Vec3d>& vertices,
const std::vector<TetraArray>& tetrahedra)
const std::vector<TetraArray>& tetrahedra,
bool computeAttachedSurfaceMesh)
{
Mesh::initialize(vertices);
this->setTetrahedraVertices(tetrahedra);
if(computeAttachedSurfaceMesh)
{
this->computeAttachedSurfaceMesh();
}
}
void
......
......@@ -51,7 +51,8 @@ public:
/// tetrahedra connectivity
///
void initialize(const std::vector<Vec3d>& vertices,
const std::vector<TetraArray>& tetrahedra);
const std::vector<TetraArray>& tetrahedra,
bool computeAttachedSurfaceMesh = false);
///
/// \brief Clear all the mesh data
......
......@@ -23,12 +23,11 @@
#include "vtkSmartPointer.h"
#include "vtkGenericDataObjectReader.h"
#include "vtkXMLGenericDataObjectReader.h"
#include "vtkXMLUnstructuredGridReader.h"
#include "vtkXMLPolyDataReader.h"
#include "vtkPLYReader.h"
#include "vtkOBJReader.h"
#include "vtkSTLReader.h"
#include "vtkPolyData.h"
#include "vtkUnstructuredGrid.h"
#include "vtkFloatArray.h"
#include "g3log/g3log.hpp"
......@@ -41,28 +40,32 @@ VTKMeshReader::read(const std::string& filePath, MeshReader::FileType meshType)
{
case MeshReader::FileType::VTK :
{
return VTKMeshReader::readAsGenericFormatData<vtkGenericDataObjectReader>(filePath);
return VTKMeshReader::readVtkGenericFormatData<vtkGenericDataObjectReader>(filePath);
break;
}
case MeshReader::FileType::VTU :
{
return VTKMeshReader::readVtkUnstructuredGrid<vtkXMLUnstructuredGridReader>(filePath);
break;
}
case MeshReader::FileType::VTP :
{
return VTKMeshReader::readAsGenericFormatData<vtkXMLGenericDataObjectReader>(filePath);
return VTKMeshReader::readVtkPolyData<vtkXMLPolyDataReader>(filePath);
break;
}
case MeshReader::FileType::STL :
{
return VTKMeshReader::readAsAbstractPolyData<vtkSTLReader>(filePath);
return VTKMeshReader::readVtkPolyData<vtkSTLReader>(filePath);
break;
}
case MeshReader::FileType::PLY :
{
return VTKMeshReader::readAsAbstractPolyData<vtkPLYReader>(filePath);
return VTKMeshReader::readVtkPolyData<vtkPLYReader>(filePath);
break;
}
case MeshReader::FileType::OBJ :
{
return VTKMeshReader::readAsAbstractPolyData<vtkOBJReader>(filePath);
return VTKMeshReader::readVtkPolyData<vtkOBJReader>(filePath);
break;
}
default :
......@@ -75,64 +78,113 @@ VTKMeshReader::read(const std::string& filePath, MeshReader::FileType meshType)
template<typename ReaderType>
std::shared_ptr<Mesh>
VTKMeshReader::readAsGenericFormatData(const std::string& filePath)
VTKMeshReader::readVtkGenericFormatData(const std::string& filePath)
{
std::vector<Vec3d> vertices;
std::vector<Vec2f> textCoords;
std::vector<SurfaceMesh::TriangleArray> triangles;
std::vector<TetrahedralMesh::TetraArray> tetrahedra;
std::vector<HexahedralMesh::HexaArray> hexahedra;
auto reader = vtkSmartPointer<ReaderType>::New();
reader->SetFileName(filePath.c_str());
reader->Update();
vtkPointSet* output = vtkPointSet::SafeDownCast(reader->GetOutput());
if(!output)
if (vtkPolyData* vtkMesh = reader->GetPolyDataOutput())
{
LOG(WARNING) << "VTKMeshReader::readAsGenericFormatData error: could not read with VTK reader.";
return nullptr;
return VTKMeshReader::convertVtkPolyDataToSurfaceMesh(vtkMesh);
}
VTKMeshReader::copyVertices(output->GetPoints(), vertices);
if(auto vtkMesh = reader->GetPolyDataOutput())
else if (vtkUnstructuredGrid* vtkMesh = reader->GetUnstructuredGridOutput())
{
VTKMeshReader::copyCells<3>(vtkMesh->GetPolys(), triangles);
return VTKMeshReader::convertVtkUnstructuredGridToVolumetricMesh(vtkMesh);
}
else if (auto vtkMesh = reader->GetUnstructuredGridOutput())
else
{
VTKMeshReader::copyCells<4>(vtkMesh->GetCells(), tetrahedra);
//TODO : And hexahedra??
LOG(WARNING) << "VTKMeshReader::readVtkGenericFormatData error: could not read with VTK reader.";
return nullptr;
}
LOG(WARNING) << "VTKMeshReader::readAsGenericFormatData not finished.";
}
template<typename ReaderType>
std::shared_ptr<SurfaceMesh>
VTKMeshReader::readAsAbstractPolyData(const std::string& filePath)
VTKMeshReader::readVtkPolyData(const std::string& filePath)
{
std::vector<Vec3d> vertices;
std::vector<Vec2f> textCoords;
std::vector<SurfaceMesh::TriangleArray> triangles;
auto reader = vtkSmartPointer<ReaderType>::New();
reader->SetFileName(filePath.c_str());
reader->Update();
vtkPolyData* vtkMesh = reader->GetOutput();
return VTKMeshReader::convertVtkPolyDataToSurfaceMesh(vtkMesh);
}
template<typename ReaderType>
std::shared_ptr<VolumetricMesh>
VTKMeshReader::readVtkUnstructuredGrid(const std::string& filePath)
{
auto reader = vtkSmartPointer<ReaderType>::New();
reader->SetFileName(filePath.c_str());
reader->Update();
vtkUnstructuredGrid* vtkMesh = reader->GetOutput();
return VTKMeshReader::convertVtkUnstructuredGridToVolumetricMesh(vtkMesh);
}
std::shared_ptr<SurfaceMesh>
VTKMeshReader::convertVtkPolyDataToSurfaceMesh(vtkPolyData* vtkMesh)
{
if(!vtkMesh)
{
LOG(WARNING) << "VTKMeshReader::readAsAbstractPolyData error: could not read with VTK reader.";
LOG(WARNING) << "VTKMeshReader::convertVtkPolyDataToSurfaceMesh error: could not read with VTK reader.";
return nullptr;
}
std::vector<Vec3d> vertices;
VTKMeshReader::copyVertices(vtkMesh->GetPoints(), vertices);
std::vector<SurfaceMesh::TriangleArray> triangles;
VTKMeshReader::copyCells<3>(vtkMesh->GetPolys(), triangles);
std::vector<Vec2f> textCoords;
VTKMeshReader::copyTextureCoordinates(vtkMesh->GetPointData(), textCoords);
return MeshReader::createSurfaceMesh(vertices, triangles, textCoords);
auto mesh = std::make_shared<SurfaceMesh>();
mesh->initialize(vertices, triangles, textCoords, true);
return mesh;
}
std::shared_ptr<VolumetricMesh>
VTKMeshReader::convertVtkUnstructuredGridToVolumetricMesh(vtkUnstructuredGrid* vtkMesh)
{
if(!vtkMesh)
{
LOG(WARNING) << "VTKMeshReader::convertVtkUnstructuredGridToVolumetricMesh error: could not read with VTK reader.";
return nullptr;
}
std::vector<Vec3d> vertices;
VTKMeshReader::copyVertices(vtkMesh->GetPoints(), vertices);
vtkIdType cellType;
vtkMesh->GetCellType(cellType);
if( cellType == VTK_TETRA )
{
std::vector<TetrahedralMesh::TetraArray> cells;
VTKMeshReader::copyCells<4>(vtkMesh->GetCells(), cells);
auto mesh = std::make_shared<TetrahedralMesh>();
mesh->initialize(vertices, cells, true);
return mesh;
}
else if( cellType == VTK_HEXAHEDRON )
{
const size_t dim = 8;
std::vector<HexahedralMesh::HexaArray> cells;
VTKMeshReader::copyCells<8>(vtkMesh->GetCells(), cells);
auto mesh = std::make_shared<HexahedralMesh>();
mesh->initialize(vertices, cells, true);
return mesh;
}
else
{
LOG(WARNING) << "VTKMeshReader::convertVtkUnstructuredGridToVolumetricMesh error: No support for vtkCellType="
<< cellType << ".";
return nullptr;
}
}
void
......
......@@ -24,6 +24,8 @@
#include <memory>
#include "vtkUnstructuredGrid.h"
#include "vtkPolyData.h"
#include "vtkPoints.h"
#include "vtkCellArray.h"
#include "vtkPointData.h"
......@@ -59,13 +61,20 @@ protected:
/// \brief
///
template<typename ReaderType>
static std::shared_ptr<Mesh> readAsGenericFormatData(const std::string& filePath);
static std::shared_ptr<Mesh> readVtkGenericFormatData(const std::string& filePath);
///
/// \brief
///
template<typename ReaderType>
static std::shared_ptr<SurfaceMesh> readAsAbstractPolyData(const std::string& filePath);
static std::shared_ptr<SurfaceMesh> readVtkPolyData(const std::string& filePath);
template<typename ReaderType>
static std::shared_ptr<VolumetricMesh> readVtkUnstructuredGrid(const std::string& filePath);
static std::shared_ptr<SurfaceMesh> convertVtkPolyDataToSurfaceMesh(vtkPolyData* vtkMesh);
static std::shared_ptr<VolumetricMesh> convertVtkUnstructuredGridToVolumetricMesh(vtkUnstructuredGrid* vtkMesh);
///
/// \brief
......
......@@ -42,12 +42,12 @@ int main()
<< "****************\n";
//testViewer();
//testReadMesh();
testReadMesh();
//testAnalyticalGeometry();
//testScenesManagement();
//testIsometricMap();
//testTetraTriangleMap();
testExtractSurfaceMesh();
//testExtractSurfaceMesh();
//testOneToOneNodalMap();
//testSurfaceMeshOptimizer();
......@@ -62,18 +62,20 @@ void testReadMesh()
scene->setLoopDelay(1000);
// Read mesh
std::string filePath = "/home/virtualfls/Projects/IMSTK/resources/Cube/models/cube.obj";
auto mesh = imstk::MeshReader::read(filePath);
auto objMesh = imstk::MeshReader::read("/home/virtualfls/Projects/IMSTK/resources/Cube/models/cube.obj");
auto plyMesh = imstk::MeshReader::read("/home/virtualfls/Projects/IMSTK/resources/Cube/models/cube.ply");
auto stlMesh = imstk::MeshReader::read("/home/virtualfls/Projects/IMSTK/resources/Cube/models/cube.stl");
auto vtkMesh = imstk::MeshReader::read("/home/virtualfls/Projects/IMSTK/resources/Cube/models/cube.vtk");
auto vtpMesh = imstk::MeshReader::read("/home/virtualfls/Projects/IMSTK/resources/Cube/models/cube.vtp");
// Create and add obj
auto obj = std::make_shared<imstk::VisualObject>("meshObject");
obj->setVisualGeometry(mesh);
scene->addSceneObject(obj);
// Create and add object
auto object = std::make_shared<imstk::VisualObject>("meshObject");
object->setVisualGeometry(objMesh);
scene->addSceneObject(object);
// Run
sdk->setCurrentScene("SceneTestMesh");
sdk->startSimulation(true);
}
void testViewer()
......@@ -458,4 +460,4 @@ void testSurfaceMeshOptimizer()
// Nodal data: 0:(0, 0, 0), 1:(0.5, 0, 0), 2:(0, 0.5, 0), 3:(0.5, 0.5, 0), 4:(0, 1, 0), 5:(1, 0, 0), 6:(0.5, 1, 0), 7:(1, 0.5, 0), 8:(1, 1, 0)
getchar();
}
\ No newline at end of file
}
Supports Markdown
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