fw4spl
SplineReaderService.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 "ioData/SplineReaderService.hpp"
8 
9 #include <fwCore/base.hpp>
10 
11 #include <fwDataTools/fieldHelper/Image.hpp>
12 
13 #include <fwIO/IReader.hpp>
14 
15 #include <fwRuntime/ConfigurationElement.hpp>
16 
17 #include <fwServices/macros.hpp>
18 
19 #include <boost/lexical_cast.hpp>
20 
21 #include <fstream>
22 #include <iostream>
23 
24 fwServicesRegisterMacro( ::fwIO::IReader, ::ioData::SplineReaderService, ::fwData::Spline );
25 
26 namespace ioData
27 {
28 
30 {
31  isTransfo = false;
32 }
33 
34 //-----------------------------------------------------------------------------
35 
36 void SplineReaderService::info(std::ostream& _sstream )
37 {
38  this->SuperClass::info( _sstream );
39  _sstream << " spline reader" << std::endl;
40 }
41 
42 //-----------------------------------------------------------------------------
43 
44 std::vector< std::string > SplineReaderService::getSupportedExtensions()
45 {
46  std::vector< std::string > extensions;
47  extensions.push_back(".spline");
48  return extensions;
49 }
50 
51 //-----------------------------------------------------------------------------
52 
54 {
55 }
56 
57 //-----------------------------------------------------------------------------
58 
60 {
62 
64  for (iter = m_configuration->begin(); iter != m_configuration->end(); ++iter)
65  {
66  SLM_INFO((*iter)->getName());
67  if ((*iter)->getName() == "spline")
68  {
69  if((*iter)->hasAttribute("id"))
70  {
71  m_idSpline = boost::lexical_cast<int >((*iter)->getExistingAttributeValue("id").c_str());
72  SLM_INFO((*iter)->getExistingAttributeValue("id"));
73  }
74  if((*iter)->hasAttribute("nbSides"))
75  {
76  m_nbSides = boost::lexical_cast<int >((*iter)->getExistingAttributeValue("nbSides").c_str());
77  SLM_INFO((*iter)->getExistingAttributeValue("nbSides"));
78  }
79  if((*iter)->hasAttribute("radius"))
80  {
81  m_radius = boost::lexical_cast<double >((*iter)->getExistingAttributeValue("radius").c_str());
82  SLM_INFO((*iter)->getExistingAttributeValue("radius"));
83  }
84  if((*iter)->hasAttribute("matrix"))
85  {
86  objectMatrix = loadObjectTransformationMatrix3D((*iter)->getExistingAttributeValue("matrix"));
87  isTransfo = true;
88  OSLM_INFO("spline matrix: " << (*iter)->getExistingAttributeValue("matrix"));
89  }
90 
92  ::fwRuntime::ConfigurationElement::sptr m_configuration2 = m_configuration->findConfigurationElement(
93  "spline");
94  for (iter2 = m_configuration2->begin(); iter2 != m_configuration2->end(); ++iter2)
95  {
96  if ((*iter2)->getName() == "point"
97  && (*iter2)->hasAttribute("id"))
98  {
100  pt.id = boost::lexical_cast<int >((*iter2)->getExistingAttributeValue("id").c_str());
101  SLM_INFO((*iter2)->getExistingAttributeValue("id"));
102  pt.p[0] = pt.id * 100.0;
103  pt.p[1] = 0.0;
104  pt.p[2] = 0.0;
105  pt.isVisible = false;
106  m_points.push_back( pt );
107  }
108  }
109  }
110  }
111 }
112 
113 //-----------------------------------------------------------------------------
114 
116 {
117  SLM_TRACE_FUNC();
118  // Retrieve object
119  ::fwData::Spline::sptr spline = this->getObject< ::fwData::Spline >( );
120 
121  spline->points() = m_points;
122  spline->setRadius(m_radius);
123  spline->setNbSides(m_nbSides);
124  spline->setIdSpline(m_idSpline);
125 
126  // Notify reading
127 
128  if(isTransfo)
129  {
130  spline->setField( ::fwDataTools::fieldHelper::Image::position, objectMatrix );
131  }
132 
134  {
135  ::fwCom::Connection::Blocker block(sig->getConnection(m_slotUpdate));
136  sig->asyncEmit();
137  }
138 }
139 
140 //-----------------------------------------------------------------------------
141 
142 ::fwData::TransformationMatrix3D::sptr SplineReaderService::loadObjectTransformationMatrix3D(std::string m_file)
143 {
144  ::fwData::TransformationMatrix3D::sptr matrix = ::fwData::TransformationMatrix3D::New();
145  ::boost::filesystem::path location(m_file);
146 
147  std::fstream file;
148  file.open(location.string().c_str(), std::fstream::in);
149  if (!file.is_open())
150  {
151  OSLM_WARN( "Object Matrix file loading error for " + location.string());
152  return matrix;
153  }
154  double val;
155  for( int l = 0; l < 4; l++ )
156  {
157  for( int c = 0; c < 4; c++ )
158  {
159  file >> val;
160  matrix->setCoefficient(l, c, val);
161  }
162  }
163  return matrix;
164 }
165 
166 }
FWDATATOOLS_API static const std::string position
position oriented IDs
Class allowing to block a Connection.
Definition: Connection.hpp:20
#define SLM_TRACE_FUNC()
Trace contextual function signature.
Definition: spyLog.hpp:329
int id
Identifier.
Definition: Spline.hpp:35
#define OSLM_INFO(message)
Definition: spyLog.hpp:252
virtual IODATA_API void configuring() override
Configure service. This method is called by configure() from base service ( fwServices::IService ) ...
UpdateSlotType::sptr m_slotUpdate
Slot to call update method.
Definition: IService.hpp:690
bool isVisible
Flag if point is visible.
Definition: Spline.hpp:39
IODATA_API void updating() override
Updating method. This method is called by update() from base service ( fwServices::IService ) ...
Reader service API. It manages extension points definition and extension configuration.
Definition: IReader.hpp:34
#define OSLM_WARN(message)
Definition: spyLog.hpp:263
::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 IODATA_API void info(std::ostream &_sstream) override
Info method.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_MODIFIED_SIG
Key in m_signals map of signal m_sigModified.
IODATA_API ~SplineReaderService() noexcept
Destructor.
#define SLM_INFO(message)
Definition: spyLog.hpp:250
The namespace ioData contains reader and writer services for basic fwData::Object which doesn&#39;t need ...
IODATA_API SplineReaderService()
Constructor.
Container::iterator Iterator
Defines the configuration element container type.
double p[3]
Point coordinates.
Definition: Spline.hpp:36
This class defines a spline object.
Definition: Spline.hpp:22
Defines a 3D point for a spline.
Definition: Spline.hpp:31
virtual IODATA_API std::vector< std::string > getSupportedExtensions() override
returns (filename) extension
virtual FWSERVICES_API void info(std::ostream &_sstream)
Write information in a stream.
Definition: IService.cpp:74