fw4spl
fwVtkIO/src/fwVtkIO/ImageWriter.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2018.
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/ImageWriter.hpp"
8 
9 #include "fwVtkIO/helper/vtkLambdaCommand.hpp"
10 #include "fwVtkIO/vtk.hpp"
11 
12 #include <fwCore/base.hpp>
13 
14 #include <fwDataIO/writer/registry/macros.hpp>
15 
16 #include <fwJobs/Observer.hpp>
17 
18 #include <vtkGenericDataObjectWriter.h>
19 #include <vtkImageData.h>
20 #include <vtkSmartPointer.h>
21 
22 fwDataIOWriterRegisterMacro( ::fwVtkIO::ImageWriter );
23 
24 namespace fwVtkIO
25 {
26 //------------------------------------------------------------------------------
27 
29  ::fwData::location::enableSingleFile< ::fwDataIO::writer::IObjectWriter >(this),
30  m_job(::fwJobs::Observer::New("VTK Image Writer"))
31 {
33 }
34 
35 //------------------------------------------------------------------------------
36 
38 {
40 }
41 
42 //------------------------------------------------------------------------------
43 
45 {
46  using namespace fwVtkIO::helper;
47 
48  assert( !m_object.expired() );
49  assert( m_object.lock() );
50 
51  ::fwData::Image::csptr pImage = getConcreteObject();
52 
53  vtkSmartPointer< vtkGenericDataObjectWriter > writer = vtkSmartPointer< vtkGenericDataObjectWriter >::New();
54  vtkSmartPointer< vtkImageData > vtkImage = vtkSmartPointer< vtkImageData >::New();
55  ::fwVtkIO::toVTKImage( pImage, vtkImage );
56  writer->SetInputData( vtkImage );
57  writer->SetFileName(this->getFile().string().c_str());
58  writer->SetFileTypeToBinary();
59 
60  vtkSmartPointer<vtkLambdaCommand> progressCallback;
61  progressCallback = vtkSmartPointer<vtkLambdaCommand>::New();
62  progressCallback->SetCallback([this](vtkObject* caller, long unsigned int, void* )
63  {
64  auto filter = static_cast<vtkGenericDataObjectWriter*>(caller);
65  m_job->doneWork( filter->GetProgress()*100 );
66  });
67 
68  writer->AddObserver(vtkCommand::ProgressEvent, progressCallback);
69  m_job->addSimpleCancelHook( [&]()
70  {
71  writer->AbortExecuteOn();
72  });
73  writer->Write();
74  m_job->finish();
75 }
76 
77 //------------------------------------------------------------------------------
78 
80 {
81  return ".vtk";
82 }
83 
84 //------------------------------------------------------------------------------
85 
87 {
88  return m_job;
89 }
90 
91 } // namespace fwVtkIO
FWVTKIO_API void write() override
Writing operator.
#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::cwptr m_object
Object write on filesystem by the process.
virtual std::shared_ptr< const DataType > getConcreteObject() const
m_object getter.
FWVTKIO_API ~ImageWriter()
Destructor.
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
FWVTKIO_API std::shared_ptr< ::fwJobs::IJob > getJob() const override
Contains the representation of the data objects used in the framework.
FWVTKIO_API ImageWriter(::fwDataIO::writer::IObjectWriter::Key key)
Constructor.
This namespace fwJobs provides jobs management.