fw4spl
TagReader.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 "fwDataIO/reader/TagReader.hpp"
8 
9 #include "fwDataIO/reader/registry/macros.hpp"
10 
11 #include <fwData/location/SingleFile.hpp>
12 #include <fwData/Point.hpp>
13 #include <fwData/PointList.hpp>
14 #include <fwData/Tag.hpp>
15 
16 #include <fwMath/IntrasecTypes.hpp>
17 
18 #include <fstream>
19 #include <iostream>
20 
21 fwDataIOReaderRegisterMacro( ::fwDataIO::reader::TagReader );
22 
23 namespace fwDataIO
24 {
25 
26 namespace reader
27 {
28 
29 //------------------------------------------------------------------------------
30 
32  ::fwData::location::enableSingleFile< IObjectReader >(this)
33 {
34 }
35 
36 //------------------------------------------------------------------------------
37 
39 {
40 }
41 
42 //------------------------------------------------------------------------------
43 
45 {
46  assert( ::std::dynamic_pointer_cast< ::fwData::location::SingleFile >(m_location) );
47  ::boost::filesystem::path path =
48  ::std::dynamic_pointer_cast< ::fwData::location::SingleFile >(m_location)->getPath();
49 
50  OSLM_INFO( "[TagReader::read] Tag file: " << path);
51  assert( path.empty() == false );
52 
53  ::std::shared_ptr< ::fwData::Tag > tag = getConcreteObject();
54 
55  std::fstream file;
56  file.open(path.string().c_str(), std::fstream::in);
57  if (!file.is_open())
58  {
59  OSLM_ERROR( "Tag file loading error for " << path.string());
60  std::string str = "Unable to open ";
61  str += path.string();
62  throw std::ios_base::failure(str);
63  }
64 
66  std::string name, type;
67  int n;
68  file>>name;
69  file>>n;
70  if(n >= 1)
71  {
72  file>>type;
73 
74  if(type == "ARTAG" || type == "CHESSBOARD" || type == "ARToolKitPlus_MARKER_ID_BCH")
75  {
76  int nbPts;
77  double x, y, z;
78  double radius = 0.0;
79  file>>x>>y>>z;
80  file>>nbPts;
81  tag->setType(type);
82  for(int i = 0; i < nbPts; i++)
83  {
84  ::fwData::Point::sptr p;
85  fwVec3d vPoint;
86  file>>vPoint[0]>>vPoint[1]>>vPoint[2]>>radius;
87  p->setCoord(vPoint);
88  tag->getPointList()->getPoints().push_back(p);
89  }
90  }
91  else
92  {
93  SLM_ERROR( "Tag file loading error for " + path.string() + " with type " + type);
94  std::string str = "Unable to open ";
95  str += path.string();
96  throw std::ios_base::failure(str);
97  }
98  }
99 
100  file.close();
101 
102 }
103 
104 //------------------------------------------------------------------------------
105 
106 std::string TagReader::extension()
107 {
108  return (".tag");
109 }
110 
111 //------------------------------------------------------------------------------
112 
113 } // namespace reader
114 
115 } // namespace fwDataIO
virtual FWDATAIO_API ~TagReader()
Destructor. Do nothing.
Definition: TagReader.cpp:38
This class defines a single file location.
Definition: SingleFile.hpp:25
FWDATAIO_API TagReader(::fwDataIO::reader::IObjectReader::Key key)
Constructor. Do nothing.
Definition: TagReader.cpp:31
This namespace fwDataIO contains reader and writer for several framework&#39;s data.
Key class used to restrict access to Object construction. See http://www.drdobbs.com/184402053.
#define OSLM_INFO(message)
Definition: spyLog.hpp:252
#define SLM_ERROR(message)
Definition: spyLog.hpp:272
#define OSLM_ERROR(message)
Definition: spyLog.hpp:274
virtual std::shared_ptr< DataType > getConcreteObject()
m_object getter.
virtual FWDATAIO_API void read() override
Read the file .tag.
Definition: TagReader.cpp:44
Contains the representation of the data objects used in the framework.
FWDATAIO_API std::string extension() override
Defines extension supported by this reader ".tag".
Definition: TagReader.cpp:106
::fwData::location::ILocation::sptr m_location
Object location ( file path, directory path, url, etc )
Tag Reader. Read file format .tag.
Definition: TagReader.hpp:35