fw4spl
fwVtkIO/src/fwVtkIO/ImageReader.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/vtk.hpp"
8 #include "fwVtkIO/ImageReader.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 <vtkImageData.h>
20 #include <vtkSmartPointer.h>
21 
22 fwDataIOReaderRegisterMacro( ::fwVtkIO::ImageReader );
23 
24 
25 namespace fwVtkIO
26 {
27 //------------------------------------------------------------------------------
28 
30  ::fwData::location::enableSingleFile< ::fwDataIO::reader::IObjectReader >(this),
31  m_job(::fwJobs::Observer::New("VTK Image reader"))
32 {
34 }
35 
36 //------------------------------------------------------------------------------
37 
39 {
41 }
42 
43 //------------------------------------------------------------------------------
44 
46 {
47  using namespace fwVtkIO::helper;
48 
49  assert( !m_object.expired() );
50  assert( m_object.lock() );
51 
52  ::fwData::Image::sptr pImage = getConcreteObject();
53 
54  vtkSmartPointer< vtkGenericDataObjectReader > reader = vtkSmartPointer< vtkGenericDataObjectReader >::New();
55  reader->SetFileName(this->getFile().string().c_str());
56 
57  vtkSmartPointer<vtkLambdaCommand> progressCallback;
58  progressCallback = vtkSmartPointer<vtkLambdaCommand>::New();
59  progressCallback->SetCallback(
60  [this](vtkObject* caller, long unsigned int, void* )
61  {
62  auto filter = static_cast<vtkGenericDataObjectReader*>(caller);
63  m_job->doneWork( filter->GetProgress()*100 );
64  }
65  );
66  reader->AddObserver(vtkCommand::ProgressEvent, progressCallback);
67 
68 
69  m_job->addSimpleCancelHook( [&]()
70  {
71  reader->AbortExecuteOn();
72  } );
73 
74  reader->Update();
75  reader->UpdateInformation();
76  reader->PropagateUpdateExtent();
77 
78  vtkDataObject *obj = reader->GetOutput();
79  vtkImageData* img = vtkImageData::SafeDownCast(obj);
80 
81  m_job->finish();
82 
83  FW_RAISE_IF("ImageReader cannot read VTK image file :"<<this->getFile().string(), !img);
84  try
85  {
86  ::fwVtkIO::fromVTKImage( img, pImage);
87  }
88  catch( std::exception &e)
89  {
90  FW_RAISE("VTKImage to fwData::Image failed "<<e.what());
91  }
92 }
93 
94 //------------------------------------------------------------------------------
95 
97 {
98  return ".vtk";
99 }
100 
101 //------------------------------------------------------------------------------
102 
104 {
105  return m_job;
106 }
107 
108 //------------------------------------------------------------------------------
109 
110 
111 } // namespace fwVtkIO
FWVTKIO_API std::shared_ptr< ::fwJobs::IJob > getJob() const override
FWVTKIO_API ImageReader(::fwDataIO::reader::IObjectReader::Key key)
Constructor.
#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.
FWVTKIO_API std::string extension() override
std::shared_ptr< ::fwJobs::IJob > sptr
Cancel request callback type.
Definition: IJob.hpp:54
FWVTKIO_API ~ImageReader()
Destructor.
FWVTKIO_API void read() override
Reading operator.
virtual std::shared_ptr< DataType > getConcreteObject()
m_object getter.
Contains the representation of the data objects used in the framework.
This namespace fwJobs provides jobs management.