fw4spl
ExternalDataReaderService.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 "ioTuto/ExternalDataReaderService.hpp"
8 
9 #include <fwData/Composite.hpp>
10 #include <fwData/location/Folder.hpp>
11 #include <fwData/location/SingleFile.hpp>
12 #include <fwData/String.hpp>
13 #include <fwData/TransformationMatrix3D.hpp>
14 
15 #include <fwDataTools/helper/Composite.hpp>
16 
17 #include <fwGui/dialog/LocationDialog.hpp>
18 #include <fwGui/dialog/MessageDialog.hpp>
19 
20 #include <fwServices/macros.hpp>
21 
22 #include <boost/filesystem/operations.hpp>
23 
24 #include <fstream>
25 #include <iostream>
26 
27 fwServicesRegisterMacro( ::fwIO::IReader, ::ioTuto::ExternalDataReaderService, ::fwData::Composite );
28 
29 namespace ioTuto
30 {
31 
32 //-----------------------------------------------------------------------------
33 
35 {
36 }
37 
38 //-----------------------------------------------------------------------------
39 
40 void ExternalDataReaderService::info(std::ostream& _sstream )
41 {
42  this->::fwIO::IReader::info( _sstream );
43  _sstream << std::endl << " External data file reader";
44 }
45 
46 //-----------------------------------------------------------------------------
47 
49 {
50  std::vector< std::string > extensions;
51  extensions.push_back(".us");
52  return extensions;
53 }
54 
55 //-----------------------------------------------------------------------------
56 
58 {
59 }
60 
61 //------------------------------------------------------------------------------
62 
64 {
65  if( m_configuration->findConfigurationElement("filename") )
66  {
67  std::string filename = m_configuration->findConfigurationElement("filename")->getValue();
68  OSLM_INFO( "ExternalDataReaderService::configure filename: " << filename );
69  this->setFile(filename);
70  }
71 }
72 
73 //------------------------------------------------------------------------------
74 
76 {
77  static ::boost::filesystem::path _sDefaultPath;
78 
80  dialogFile.setTitle("Choose an external data file");
81  dialogFile.setDefaultLocation( ::fwData::location::Folder::New(_sDefaultPath) );
82  dialogFile.addFilter("us", "*.us");
83  dialogFile.setOption(::fwGui::dialog::ILocationDialog::READ);
84  dialogFile.setOption(::fwGui::dialog::ILocationDialog::FILE_MUST_EXIST);
85 
86  ::fwData::location::SingleFile::sptr result;
87  result = ::fwData::location::SingleFile::dynamicCast( dialogFile.show() );
88  if (result)
89  {
90  _sDefaultPath = result->getPath();
91  this->setFile(result->getPath());
92  }
93  else
94  {
95  this->clearLocations();
96  }
97 }
98 
99 //------------------------------------------------------------------------------
100 
102 {
103  this->configureWithIHM();
104 
105  std::string imageName;
106  ::fwData::Composite::sptr dataComposite = this->getObject< ::fwData::Composite >();
107  ::fwDataTools::helper::Composite compositeHelper(dataComposite);
108  SLM_ASSERT("dataComposite not instanced", dataComposite);
109  try
110  {
111  if (this->hasLocationDefined())
112  {
113  // reading of the file
114  std::fstream file;
115  file.open(this->getFile().string().c_str(), std::fstream::in);
116  if (!file.is_open())
117  {
118  OSLM_ERROR( "External data file loading error for " << this->getFile());
119  std::string str = "Unable to open ";
120  str += this->getFile().string();
121  throw std::ios_base::failure(str);
122  }
123  file >> imageName;
124  int readedValue = 0;
125  double value;
126  ::fwData::TransformationMatrix3D::sptr transformation1 = ::fwData::TransformationMatrix3D::New();
127  ::fwData::TransformationMatrix3D::sptr transformation2 = ::fwData::TransformationMatrix3D::New();
128  while(!file.eof())
129  {
130  readedValue = 0;
131  while ( !file.eof() && readedValue < 32 )
132  {
133  file >> value;
134  if (readedValue < 16)
135  {
136  transformation1->getCoefficients()[readedValue] = value;
137  }
138  else
139  {
140  transformation2->getCoefficients()[readedValue] = value;
141  }
142  readedValue++;
143  }
144  }
145  file.close();
146  // TF1 contains the first and third transformations
147  if(dataComposite->find("TF1") == dataComposite->end() )
148  {
149  compositeHelper.add("TF1", transformation1);
150  }
151  else
152  {
153  compositeHelper.swap("TF1", transformation1);
154  }
155  // TF2 contains the first and third transformations
156  if(dataComposite->find("TF2") == dataComposite->end() )
157  {
158  compositeHelper.add("TF2", transformation2);
159  }
160  else
161  {
162  compositeHelper.swap("TF2", transformation2);
163  }
164  ::fwData::String::sptr imageNameStr = ::fwData::String::New(imageName);
165  if(dataComposite->find("filename") == dataComposite->end() )
166  {
167  compositeHelper.add("filename", imageNameStr);
168  }
169  else
170  {
171  compositeHelper.swap("filename", imageNameStr);
172  }
173  SLM_ASSERT("Unable to open '"+this->getFile().string()+"'.", readedValue == 32 );
174  }
175  }
176  catch(std::ios_base::failure& exception)
177  {
178  OSLM_ERROR( "External data file loading error for " << exception.what());
179  }
180 }
181 
182 //-----------------------------------------------------------------------------
183 
185 {
186  return ::fwIO::FILE;
187 }
188 
189 //------------------------------------------------------------------------------
190 
191 }
FWIO_API bool hasLocationDefined() const
Returns if a location has been defined ( by the configuration process or directly by user ) ...
Definition: IReader.cpp:233
Defines an helper to modify an fwData::Composite and create in parallel the message to announce this ...
IOTUTO_API ExternalDataReaderService()
Constructor : does nothing.
virtual FWGUI_API void setDefaultLocation(::fwData::location::ILocation::sptr loc) override
Set the initial location for the dialog.
#define OSLM_INFO(message)
Definition: spyLog.hpp:252
virtual IOTUTO_API void configuring() override
Configure service. This method is called by configure() from base service ( fwServices::IService ) ...
FWGUI_API::fwGui::dialog::ILocationDialog & setOption(::fwGui::dialog::ILocationDialog::Options option) override
allow to set option to the file dialog mode=READ/WRITE, check=FILE_MUST_EXIST
FWGUI_API::fwData::location::ILocation::sptr show() override
Display the dialog.
FWGUI_API void addFilter(const std::string &filterName, const std::string &wildcardList) override
specify some filtering when browsing files:
IOTUTO_API::fwIO::IOPathType getIOPathType() const override
Returns managed path type, here service manages only single file.
FWIO_APIconst::boost::filesystem::path & getFile() const
Returns the file path set by the user or set during service configuration.
Definition: IReader.cpp:62
FWIO_API void setFile(const ::boost::filesystem::path &file)
Sets file path.
Definition: IReader.cpp:71
#define OSLM_ERROR(message)
Definition: spyLog.hpp:274
IOTUTO_API ~ExternalDataReaderService() noexcept
Destructor.
Reader service API. It manages extension points definition and extension configuration.
Definition: IReader.hpp:34
#define SLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:308
::fwRuntime::ConfigurationElement::sptr m_configuration
Configuration element used to configure service internal state using a generic XML like structure TOD...
Definition: IService.hpp:670
virtual IOTUTO_API std::vector< std::string > getSupportedExtensions() override
returns (filename) extension
IOPathType
IOPathType defines different type of paths used by service readers/writers.
Definition: ioTypes.hpp:19
IOTUTO_API void updating() override
Updating method. This method is called by update() from base service ( fwServices::IService ) ...
FWGUI_API void setTitle(const std::string &title) override
Set the title for the dialog.
Defines the generic file/folder selector dialog for IHM.
This class defines a composite object.
IOTUTO_API void configureWithIHM() override
Configure the image path.
virtual IOTUTO_API void info(std::ostream &_sstream) override
Info method.
FWIO_API void clearLocations()
Clear any location set by the setFile/setFiles/setFolder setter.
Definition: IReader.cpp:137
virtual FWSERVICES_API void info(std::ostream &_sstream)
Write information in a stream.
Definition: IService.cpp:74