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