fw4spl
io/fwVtkIO/src/fwVtkIO/MeshReader.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2015.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include "fwVtkIO/helper/Mesh.hpp"
8 #include "fwVtkIO/MeshReader.hpp"
9 #include "fwVtkIO/helper/vtkLambdaCommand.hpp"
10 
11 #include <fwCore/base.hpp>
12 
13 #include <fwDataIO/reader/registry/macros.hpp>
14 
15 #include <fwJobs/IJob.hpp>
16 #include <fwJobs/Observer.hpp>
17 
18 #include <vtkGenericDataObjectReader.h>
19 #include <vtkPolyData.h>
20 #include <vtkSmartPointer.h>
21 
22 fwDataIOReaderRegisterMacro( ::fwVtkIO::MeshReader );
23 
24 namespace fwVtkIO
25 {
26 //------------------------------------------------------------------------------
27 
29  ::fwData::location::enableSingleFile< ::fwDataIO::reader::IObjectReader >(this),
30  m_job(::fwJobs::Observer::New("Mesh reader"))
31 {
33 }
34 
35 //------------------------------------------------------------------------------
36 
38 {
40 }
41 
42 //------------------------------------------------------------------------------
43 
45 {
46  assert( !m_object.expired() );
47  assert( m_object.lock() );
48 
49  ::fwData::Mesh::sptr pMesh = getConcreteObject();
50 
51  using namespace fwVtkIO::helper;
52  vtkSmartPointer< vtkGenericDataObjectReader > reader = vtkSmartPointer< vtkGenericDataObjectReader >::New();
53  reader->SetFileName(this->getFile().string().c_str());
54 
55  vtkSmartPointer< vtkLambdaCommand > progressCallback;
56 
57  progressCallback = vtkSmartPointer< vtkLambdaCommand >::New();
58  progressCallback->SetCallback(
59  [&](vtkObject* caller, long unsigned int, void*)
60  {
61  auto filter = static_cast< vtkGenericDataObjectReader* >(caller);
62  m_job->doneWork(filter->GetProgress() * 100);
63  }
64  );
65  reader->AddObserver(vtkCommand::ProgressEvent, progressCallback);
66 
67  m_job->addSimpleCancelHook([&] { reader->AbortExecuteOn(); });
68 
69  reader->Update();
70 
71  vtkDataObject *obj = reader->GetOutput();
72  vtkPolyData* mesh = vtkPolyData::SafeDownCast(obj);
73  FW_RAISE_IF("MeshReader cannot read VTK Mesh file : "<< this->getFile().string(), !mesh);
75 
76  m_job->finish();
77 }
78 
79 //------------------------------------------------------------------------------
80 
81 std::string MeshReader::extension()
82 {
83  return ".vtk";
84 }
85 
86 //------------------------------------------------------------------------------
87 
89 {
90  return m_job;
91 }
92 
93 //------------------------------------------------------------------------------
94 
95 } // namespace fwVtkIO
FWVTKIO_API void read() override
Reading operator.
FWVTKIO_API ~MeshReader()
Destructor.
#define SLM_TRACE_FUNC()
Trace contextual function signature.
Definition: spyLog.hpp:329
This namespace fwDataIO contains reader and writer for several framework&#39;s data.
::fwTools::Object::wptr m_object
Object result of reading process.
Key class used to restrict access to Object construction. See http://www.drdobbs.com/184402053.
std::shared_ptr< ::fwJobs::IJob > sptr
Cancel request callback type.
Definition: IJob.hpp:54
FWVTKIO_API std::string extension() override
virtual std::shared_ptr< DataType > getConcreteObject()
m_object getter.
Contains the representation of the data objects used in the framework.
static FWVTKIO_API void fromVTKMesh(vtkSmartPointer< vtkPolyData > _polyData,::fwData::Mesh::sptr _mesh)
Convert a vtkPolyData to a ::fwData::Mesh::sptr.
FWVTKIO_API std::shared_ptr< ::fwJobs::IJob > getJob() const override
FWVTKIO_API MeshReader(::fwDataIO::reader::IObjectReader::Key key)
Constructor.
This namespace fwJobs provides jobs management.