fw4spl
ModelSeriesObjWriter.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/ModelSeriesObjWriter.hpp"
8 
9 #include "fwVtkIO/helper/Mesh.hpp"
10 #include "fwVtkIO/vtk.hpp"
11 
12 #include <fwCore/base.hpp>
13 
14 #include <fwData/Material.hpp>
15 #include <fwData/Reconstruction.hpp>
16 
17 #include <fwDataIO/writer/registry/macros.hpp>
18 
19 #include <fwJobs/IJob.hpp>
20 #include <fwJobs/Observer.hpp>
21 
22 #include <fwMedData/ModelSeries.hpp>
23 
24 #include <fwTools/UUID.hpp>
25 
26 #include <boost/filesystem.hpp>
27 
28 #include <vtkActor.h>
29 #include <vtkOBJExporter.h>
30 #include <vtkPolyData.h>
31 #include <vtkPolyDataMapper.h>
32 #include <vtkProperty.h>
33 #include <vtkRenderer.h>
34 #include <vtkRenderWindow.h>
35 #include <vtkSmartPointer.h>
36 
37 fwDataIOWriterRegisterMacro( ::fwVtkIO::ModelSeriesObjWriter );
38 
39 namespace fwVtkIO
40 {
41 //------------------------------------------------------------------------------
42 
44  ::fwData::location::enableFolder< ::fwDataIO::writer::IObjectWriter >(this),
45  m_job(::fwJobs::Observer::New("ModelSeries Writer"))
46 {
48 }
49 
50 //------------------------------------------------------------------------------
51 
53 {
55 }
56 
57 //------------------------------------------------------------------------------
58 
59 vtkSmartPointer< vtkActor > createActor( const ::fwData::Reconstruction::sptr& pReconstruction )
60 {
61  vtkSmartPointer< vtkActor > actor = vtkSmartPointer< vtkActor >::New();
62 
63  ::fwData::Mesh::sptr mesh = pReconstruction->getMesh();
64  ::fwData::Material::sptr material = pReconstruction->getMaterial();
65 
66  vtkSmartPointer< vtkPolyData > polyData = vtkSmartPointer< vtkPolyData >::New();
67  ::fwVtkIO::helper::Mesh::toVTKMesh( mesh, polyData);
68  vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
69  mapper->SetInputData(polyData);
70  actor->SetMapper(mapper);
71 
72  vtkProperty* property = actor->GetProperty();
73 
74  ::fwData::Color::sptr diffuse = material->diffuse();
75  property->SetDiffuseColor(diffuse->red(), diffuse->green(), diffuse->blue());
76  property->SetOpacity( diffuse->alpha() );
77 
78  ::fwData::Color::sptr ambient = material->ambient();
79  property->SetAmbientColor(ambient->red(), ambient->green(), ambient->blue());
80 
81  property->SetSpecularColor(1., 1., 1.);
82  property->SetSpecularPower(100.); //Shininess
83 
84  property->SetInterpolationToPhong();
85 
86  return actor;
87 }
88 
89 //------------------------------------------------------------------------------
90 
92 {
93  assert( !m_object.expired() );
94  assert( m_object.lock() );
95 
96  ::boost::filesystem::path prefix = this->getFolder();
97 
98  ::fwMedData::ModelSeries::csptr modelSeries = getConcreteObject();
99 
100  m_job->setTotalWorkUnits(modelSeries->getReconstructionDB().size());
101  std::uint64_t units = 0;
102  for(const ::fwData::Reconstruction::sptr& rec : modelSeries->getReconstructionDB() )
103  {
104  vtkSmartPointer< vtkRenderer > renderer = vtkSmartPointer< vtkRenderer >::New();
105  vtkSmartPointer< vtkActor > actor = createActor(rec);
106  renderer->AddActor(actor);
107 
108  vtkSmartPointer< vtkRenderWindow > renderWindow = vtkSmartPointer< vtkRenderWindow >::New();
109  renderWindow->AddRenderer(renderer);
110 
111  std::string filename = (prefix / (rec->getOrganName() + "_" + ::fwTools::UUID::get(rec))).string();
112 
113  vtkSmartPointer< vtkOBJExporter > exporter = vtkSmartPointer< vtkOBJExporter >::New();
114  exporter->SetRenderWindow(renderWindow);
115  exporter->SetFilePrefix(filename.c_str());
116  exporter->Write();
117  m_job->doneWork(++units);
118  // can not observe progression, not a vtkAlgorithm ...
119  }
120  m_job->finish();
121 }
122 
123 //------------------------------------------------------------------------------
124 
126 {
127  return ".obj";
128 }
129 
130 //------------------------------------------------------------------------------
131 
133 {
134  return m_job;
135 }
136 
137 } // namespace fwVtkIO
ILocation::PathType getFolder()
Get folder filesystem path.
Definition: Folder.hpp:99
#define SLM_TRACE_FUNC()
Trace contextual function signature.
Definition: spyLog.hpp:329
FWVTKIO_API ~ModelSeriesObjWriter()
Destructor.
static FWVTKIO_API void toVTKMesh(const ::fwData::Mesh::csptr &_mesh, vtkSmartPointer< vtkPolyData > _polyData)
Convert a ::fwData::Mesh::csptr to a vtkPolyData.
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.
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 ModelSeriesObjWriter(::fwDataIO::writer::IObjectWriter::Key key)
Constructor.
Write a fwData::Reconstruction.
static FWTOOLS_API const UUIDType & get(::fwTools::Object::sptr object)
Return an uuid to the given object : if no one previously set then generate a new one...
Definition: UUID.cpp:50
FWVTKIO_API std::string extension() override
Contains the representation of the data objects used in the framework.
FWVTKIO_API std::shared_ptr< ::fwJobs::IJob > getJob() const override
FWVTKIO_API void write() override
Writing operator.
This namespace fwJobs provides jobs management.