Commit 9e39b921 authored by Alexis Girault's avatar Alexis Girault
Browse files

WIP: Improve API for VTKMeshReader

1) readAsAbstractPolyData completed
2) readAsGenericFormatData still ongoing
3) implemented standard createSurfaceMesh in MeshReader
parent 1d819fa6
......@@ -43,17 +43,40 @@ MeshReader::read(const std::string& filePath)
case FileType::STL :
case FileType::PLY :
case FileType::OBJ :
LOG(DEBUG) << "Read using VTK Mesh reader";
VTKMeshReader::read(filePath, meshType);
LOG(DEBUG) << "MeshReader::read debug: Read using VTK Mesh reader.";
return VTKMeshReader::read(filePath, meshType);
break;
case FileType::VEG :
LOG(DEBUG) << "Read using Vega Mesh reader";
LOG(DEBUG) << "MeshReader::read debug: Read using Vega Mesh reader.";
LOG(WARNING) << "MeshReader::read error: vega reader not yet implemented.";
return nullptr;
break;
}
LOG(WARNING) << "MeshReader::read error: file type not supported";
return nullptr;
}
std::shared_ptr<SurfaceMesh>
MeshReader::createSurfaceMesh(const std::vector<Vec3d>& vertices,
const std::vector<SurfaceMesh::TriangleArray>& triangles,
const std::vector<Vec2f>& textCoords)
{
auto surfaceMesh = std::make_shared<SurfaceMesh>();
surfaceMesh->setInitialVerticesPositions(vertices);
surfaceMesh->setVerticesPositions(vertices);
surfaceMesh->setTrianglesVertices(triangles);
surfaceMesh->setTextureCoordinates(textCoords);
surfaceMesh->computeVerticesNormals();
if(!textCoords.empty())
{
surfaceMesh->computeVerticesTangents();
}
return surfaceMesh;
}
const MeshReader::FileType
MeshReader::getFileType(const std::string& filePath)
{
......@@ -101,4 +124,5 @@ MeshReader::getFileType(const std::string& filePath)
return meshType;
}
}
......@@ -25,6 +25,7 @@
#include <memory>
#include "imstkMesh.h"
#include "imstkSurfaceMesh.h"
namespace imstk {
......@@ -49,6 +50,10 @@ public:
static std::shared_ptr<Mesh> read(const std::string& filePath);
static std::shared_ptr<SurfaceMesh> createSurfaceMesh(const std::vector<Vec3d>& vertices,
const std::vector<SurfaceMesh::TriangleArray>& triangles,
const std::vector<Vec2f>& textCoords);
protected:
static const FileType getFileType(const std::string& filePath);
......
......@@ -29,6 +29,7 @@
#include "vtkSTLReader.h"
#include "vtkPolyData.h"
#include "vtkUnstructuredGrid.h"
#include "vtkFloatArray.h"
#include "g3log/g3log.hpp"
......@@ -36,41 +37,32 @@ namespace imstk {
std::shared_ptr<Mesh>
VTKMeshReader::read(const std::string& filePath, MeshReader::FileType meshType)
{
std::vector<Vec3d> vertices;
std::vector<Vec2f> textCoords;
std::vector<SurfaceMesh::TriangleArray> triangles;
std::vector<TetrahedralMesh::TetraArray> tetrahedra;
std::vector<HexahedralMesh::HexaArray> hexahedra;
// Populate vectors and arrays
switch (meshType)
{
case MeshReader::FileType::VTK :
{
VTKMeshReader::readAsGenericFormatData<vtkGenericDataObjectReader>
(filePath, vertices, triangles, tetrahedra, hexahedra);
return VTKMeshReader::readAsGenericFormatData<vtkGenericDataObjectReader>(filePath);
break;
}
case MeshReader::FileType::VTU :
case MeshReader::FileType::VTP :
{
VTKMeshReader::readAsGenericFormatData<vtkXMLGenericDataObjectReader>
(filePath, vertices, triangles, tetrahedra, hexahedra);
return VTKMeshReader::readAsGenericFormatData<vtkXMLGenericDataObjectReader>(filePath);
break;
}
case MeshReader::FileType::STL :
{
VTKMeshReader::readAsAbstractPolyData<vtkSTLReader>(filePath, vertices, triangles);
return VTKMeshReader::readAsAbstractPolyData<vtkSTLReader>(filePath);
break;
}
case MeshReader::FileType::PLY :
{
VTKMeshReader::readAsAbstractPolyData<vtkSTLReader>(filePath, vertices, triangles);
return VTKMeshReader::readAsAbstractPolyData<vtkPLYReader>(filePath);
break;
}
case MeshReader::FileType::OBJ :
{
VTKMeshReader::readAsAbstractPolyData<vtkSTLReader>(filePath, vertices, triangles);
return VTKMeshReader::readAsAbstractPolyData<vtkOBJReader>(filePath);
break;
}
default :
......@@ -82,13 +74,15 @@ VTKMeshReader::read(const std::string& filePath, MeshReader::FileType meshType)
}
template<typename ReaderType>
void
VTKMeshReader::readAsGenericFormatData(const std::string& filePath,
std::vector<Vec3d>& vertices,
std::vector<SurfaceMesh::TriangleArray>& triangles,
std::vector<TetrahedralMesh::TetraArray>& tetrahedra,
std::vector<HexahedralMesh::HexaArray>& hexahedra)
std::shared_ptr<Mesh>
VTKMeshReader::readAsGenericFormatData(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();
......@@ -97,7 +91,7 @@ VTKMeshReader::readAsGenericFormatData(const std::string& filePath,
if(!output)
{
LOG(WARNING) << "VTKMeshReader::readAsGenericFormatData error: could not read with VTK reader.";
return;
return nullptr;
}
VTKMeshReader::copyVertices(output->GetPoints(), vertices);
......@@ -116,11 +110,13 @@ VTKMeshReader::readAsGenericFormatData(const std::string& filePath,
}
template<typename ReaderType>
void
VTKMeshReader::readAsAbstractPolyData(const std::string& filePath,
std::vector<Vec3d>& vertices,
std::vector<SurfaceMesh::TriangleArray>& triangles)
std::shared_ptr<SurfaceMesh>
VTKMeshReader::readAsAbstractPolyData(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();
......@@ -129,13 +125,14 @@ VTKMeshReader::readAsAbstractPolyData(const std::string& filePath,
if(!vtkMesh)
{
LOG(WARNING) << "VTKMeshReader::readAsAbstractPolyData error: could not read with VTK reader.";
return;
return nullptr;
}
VTKMeshReader::copyVertices(vtkMesh->GetPoints(), vertices);
VTKMeshReader::copyCells<3>(vtkMesh->GetPolys(), triangles);
VTKMeshReader::copyTextureCoordinates(vtkMesh->GetPointData(), textCoords);
LOG(WARNING) << "VTKMeshReader::readAsAbstractPolyData not finished.";
return MeshReader::createSurfaceMesh(vertices, triangles, textCoords);
}
void
......@@ -177,4 +174,27 @@ VTKMeshReader::copyCells(vtkCellArray* vtkCells, std::vector<std::array<size_t,d
cells.emplace_back(cell);
}
}
void
VTKMeshReader::copyTextureCoordinates(vtkPointData* pointData, std::vector<Vec2f>& textCoords)
{
if(!pointData)
{
return;
}
auto tcoords = vtkFloatArray::SafeDownCast(pointData->GetTCoords());
if(!tcoords)
{
return;
}
for(vtkIdType i = 0; i < tcoords->GetNumberOfTuples(); ++i)
{
float uv[2];
tcoords->GetTupleValue(i, uv);
textCoords.emplace_back(uv[0], uv[1]);
}
}
}
......@@ -26,6 +26,7 @@
#include "vtkPoints.h"
#include "vtkCellArray.h"
#include "vtkPointData.h"
//#include "vtkFieldData.h"
#include "imstkMeshReader.h"
......@@ -47,21 +48,18 @@ public:
protected:
template<typename ReaderType>
static void readAsGenericFormatData(const std::string& filePath,
std::vector<Vec3d>& vertices,
std::vector<SurfaceMesh::TriangleArray>& triangles,
std::vector<TetrahedralMesh::TetraArray>& tetrahedra,
std::vector<HexahedralMesh::HexaArray>& hexahedra);
static std::shared_ptr<Mesh> readAsGenericFormatData(const std::string& filePath);
template<typename ReaderType>
static void readAsAbstractPolyData(const std::string& filePath,
std::vector<Vec3d>& vertices,
std::vector<SurfaceMesh::TriangleArray>& triangles);
static std::shared_ptr<SurfaceMesh> readAsAbstractPolyData(const std::string& filePath);
static void copyVertices(vtkPoints* points, std::vector<Vec3d>& vertices);
template<size_t dim>
static void copyCells(vtkCellArray* vtkCells, std::vector<std::array<size_t,dim>>& cells);
static void copyTextureCoordinates(vtkPointData* pointData, std::vector<Vec2f>& textCoords);
//static void copyData(vtkFieldData* fields, ...);
};
}
......
......@@ -16,6 +16,7 @@
// Geometry
#include "imstkTetrahedralMesh.h"
#include "imstkSurfaceMesh.h"
#include "imstkMeshReader.h"
// Map
#include "imstkTetraTriangleMap.h"
......@@ -24,6 +25,7 @@
#include "g3log/g3log.hpp"
void testViewer();
void testReadMesh();
void testAnalyticalGeometry();
void testScenesManagement();
void testIsometricMap();
......@@ -36,14 +38,37 @@ int main()
<< "****************\n";
//testViewer();
testReadMesh();
//testAnalyticalGeometry();
//testScenesManagement();
//testIsometricMap();
testTetraTriangleMap();
//testTetraTriangleMap();
return 0;
}
void testReadMesh()
{
// SDK and Scene
auto sdk = std::make_shared<imstk::SimulationManager>();
auto scene = sdk->createNewScene("SceneTestMesh");
scene->setLoopDelay(1000);
// Read mesh
std::string filePath = "/home/virtualfls/Projects/IMSTK/resources/Cube/models/cube.obj";
auto mesh = imstk::MeshReader::read(filePath);
// Create and add obj
auto obj = std::make_shared<imstk::VisualObject>("meshObject");
obj->setVisualGeometry(mesh);
scene->addSceneObject(obj);
// Run
sdk->setCurrentScene("SceneTestMesh");
sdk->startSimulation(true);
}
void testViewer()
{
// SDK and Scene
......
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