fw4spl
fwAtomConversion/src/fwAtomConversion/mapper/Landmarks.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2017.
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 "fwAtomConversion/mapper/Landmarks.hpp"
8 
9 #include "fwAtomConversion/convert.hpp"
10 #include "fwAtomConversion/exception/ConversionNotManaged.hpp"
11 #include "fwAtomConversion/mapper/registry/macros.hpp"
12 
13 #include <fwAtoms/Boolean.hpp>
14 #include <fwAtoms/Map.hpp>
15 #include <fwAtoms/Numeric.hpp>
16 #include <fwAtoms/Numeric.hxx>
17 #include <fwAtoms/Sequence.hpp>
18 #include <fwAtoms/String.hpp>
19 
20 #include <fwData/Landmarks.hpp>
21 
22 #include <fwTools/UUID.hpp>
23 
24 #include <boost/algorithm/string.hpp>
25 
26 namespace fwAtomConversion
27 {
28 namespace mapper
29 {
30 
31 //-----------------------------------------------------------------------------
32 
33 fwAtomConversionRegisterMacro( ::fwAtomConversion::mapper::Landmarks, ::fwData::Landmarks);
34 
35 //-----------------------------------------------------------------------------
36 
37 ::fwAtoms::Object::sptr Landmarks::convert( ::fwData::Object::sptr object,
38  DataVisitor::AtomCacheType& cache )
39 {
40  const camp::Class& metaclass = ::camp::classByName( object->getClassname() );
41  ::fwAtomConversion::DataVisitor visitor( object, cache );
42  metaclass.visit(visitor);
43  ::fwAtoms::Object::sptr atom = visitor.getAtomObject();
44 
45  ::fwData::Landmarks::csptr landmarks = ::fwData::Landmarks::dynamicCast(object);
46 
47  ::fwAtoms::Map::sptr map = ::fwAtoms::Map::New();
48 
49  ::fwData::Landmarks::GroupNameContainer names = landmarks->getGroupNames();
50  for (const auto& name: names)
51  {
52  const ::fwData::Landmarks::LandmarksGroup& group = landmarks->getGroup(name);
53  ::fwAtoms::Object::sptr atomGroup = ::fwAtoms::Object::New();
54  atomGroup->setMetaInfo("ID_METAINFO", ::fwTools::UUID::generateUUID());
55 
56  const std::string colorStr = std::to_string(group.m_color[0]) + ";" +
57  std::to_string(group.m_color[1]) + ";" +
58  std::to_string(group.m_color[2]) + ";" +
59  std::to_string(group.m_color[3]);
60  atomGroup->setAttribute("color", ::fwAtoms::String::New(colorStr));
61  atomGroup->setAttribute("size", ::fwAtoms::Numeric::New(group.m_size));
62  const std::string shapeStr = (group.m_shape == ::fwData::Landmarks::Shape::SPHERE) ? "SPHERE" : "CUBE";
63  atomGroup->setAttribute("shape", ::fwAtoms::String::New(shapeStr));
64  atomGroup->setAttribute("visibility", ::fwAtoms::Boolean::New(group.m_visibility));
65 
66  ::fwAtoms::Sequence::sptr seq = ::fwAtoms::Sequence::New();
67 
68  for (const auto& point : group.m_points)
69  {
70  const std::string pointStr = std::to_string(point[0]) + ";" +
71  std::to_string(point[1]) + ";" +
72  std::to_string(point[2]);
73  seq->push_back(::fwAtoms::String::New(pointStr));
74  }
75  atomGroup->setAttribute("points", seq);
76  map->insert(name, atomGroup);
77  }
78  atom->setAttribute("landmarks", map );
79 
80  return atom;
81 }
82 
83 //-----------------------------------------------------------------------------
84 
85 ::fwData::Object::sptr Landmarks::convert( ::fwAtoms::Object::sptr atom,
86  AtomVisitor::DataCacheType& cache,
87  const AtomVisitor::IReadPolicy& uuidPolicy
88  )
89 {
90  ::fwAtomConversion::AtomVisitor visitor( atom, cache, uuidPolicy );
91  visitor.visit();
92  ::fwData::Object::sptr data = visitor.getDataObject();
93  ::fwData::Landmarks::sptr landmarks = ::fwData::Landmarks::dynamicCast(data);
94 
95  ::fwAtoms::Map::sptr map = ::fwAtoms::Map::dynamicCast(atom->getAttribute("landmarks"));
96 
97  for (const auto& elt : map->getValue())
98  {
99  const std::string name = elt.first;
100  FW_RAISE_EXCEPTION_IF( exception::ConversionNotManaged(
101  "sub atoms stored in fwAtom::Map 'landmarks' must be atom objects"),
102  elt.second->type() != ::fwAtoms::Base::OBJECT );
103  ::fwAtoms::Object::sptr obj = ::fwAtoms::Object::dynamicCast(elt.second);
104 
105  // get color
106  ::fwAtoms::String::csptr colorObj = ::fwAtoms::String::dynamicCast(obj->getAttribute("color"));
107  FW_RAISE_EXCEPTION_IF( exception::ConversionNotManaged(
108  "sub atom 'color' stored in fwAtom::Object 'landmarks' must be ::fwAtoms::String"),
109  !colorObj );
110 
111  const std::string& colorStr = colorObj->getValue();
112 
113  std::vector< std::string> result;
114  ::boost::split(result, colorStr, ::boost::is_any_of(";"));
115 
116  FW_RAISE_EXCEPTION_IF( exception::ConversionNotManaged("'color' atom must be of type rgba"),
117  result.size() != 4 );
118  const ::fwData::Landmarks::ColorType color = {{
119  std::stof(result[0]), std::stof(result[1]),
120  std::stof(result[2]), std::stof(result[3])
121  }};
122 
123  // get size
124  ::fwAtoms::Numeric::csptr sizeObj = ::fwAtoms::Numeric::dynamicCast(obj->getAttribute("size"));
125  FW_RAISE_EXCEPTION_IF( exception::ConversionNotManaged(
126  "sub atom 'size' stored in fwAtom::Object 'landmarks' must be ::fwAtoms::Numeric"),
127  !sizeObj );
128  const ::fwData::Landmarks::SizeType size = sizeObj->getValue< ::fwData::Landmarks::SizeType >();
129 
130  // get shape
131  ::fwAtoms::String::csptr shapeObj = ::fwAtoms::String::dynamicCast(obj->getAttribute("shape"));
132  FW_RAISE_EXCEPTION_IF( exception::ConversionNotManaged(
133  "sub atom 'shape' stored in fwAtom::Object 'landmarks' must be ::fwAtoms::String"),
134  !shapeObj );
135 
136  const std::string& shapeStr = shapeObj->getValue();
137  ::fwData::Landmarks::Shape shape;
138  if (shapeStr == "SPHERE")
139  {
140  shape = ::fwData::Landmarks::Shape::SPHERE;
141  }
142  else if (shapeStr == "CUBE")
143  {
144  shape = ::fwData::Landmarks::Shape::CUBE;
145  }
146  else
147  {
148  FW_RAISE_EXCEPTION(exception::ConversionNotManaged("'shape' value '"+ shapeStr +"' is not managed"));
149  }
150 
151  // get visibility
152  ::fwAtoms::Boolean::csptr visuObj = ::fwAtoms::Boolean::dynamicCast(obj->getAttribute("visibility"));
153  FW_RAISE_EXCEPTION_IF( exception::ConversionNotManaged(
154  "sub atom 'visibility' stored in 'landmarks' must be ::fwAtoms::Boolean"),
155  !visuObj );
156  const bool visibility = visuObj->getValue();
157 
158  landmarks->addGroup(name, color, size, shape, visibility);
159 
160  // get points
161  ::fwAtoms::Sequence::csptr seq = ::fwAtoms::Sequence::dynamicCast(obj->getAttribute("points"));
162  for (const auto& elt : seq->getValue())
163  {
164  FW_RAISE_EXCEPTION_IF( exception::ConversionNotManaged(
165  "sub atoms stored in 'points' must be ::fwAtoms::String"),
166  elt->type() != ::fwAtoms::Base::STRING );
167 
168  ::fwAtoms::String::csptr ptStrObj = ::fwAtoms::String::dynamicCast(elt);
169  const std::string& ptStr = ptStrObj->getValue();
170 
171  std::vector< std::string> resultPt;
172  ::boost::split(resultPt, ptStr, ::boost::is_any_of(";"));
173 
174  FW_RAISE_EXCEPTION_IF( exception::ConversionNotManaged("point atom must be of type x;y;z"),
175  resultPt.size() != 3 );
176 
177  ::fwData::Landmarks::PointType pt = {{
178  std::stod(resultPt[0]), std::stod(resultPt[1]),
179  std::stod(resultPt[2])
180  }};
181  landmarks->addPoint(name, pt);
182  }
183  }
184 
185  return landmarks;
186 }
187 
188 //-----------------------------------------------------------------------------
189 
190 } //namespace mapper
191 } //namespace fwAtomConversion
virtual FWATOMCONVERSION_API std::shared_ptr< ::fwAtoms::Object > convert(std::shared_ptr< ::fwData::Object > object, DataVisitor::AtomCacheType &cache)
Convert a fwData::Object to a fwAtoms::Object.
This class defines a list of 3D points inside groups.
This namespace contains the necessary class for fwData <-> fwAtoms conversion.
static FWATOMS_API String::sptr New(std::string value)
Construct a new Object represented a string.
static Numeric::sptr New(T value)
Build a new numeric type.
Definition: Numeric.hxx:77
Visitor used to convert a fwData to a fwAtoms. fwData camp property names are used like key to store ...
Definition: DataVisitor.hpp:38
static FWATOMS_API Boolean::sptr New(std::string value)
Construct an object storing a bool value.
This class is used to convert a fwAtoms to a fwData.
Definition: AtomVisitor.hpp:31
static FWTOOLS_API UUIDType generateUUID()
Return a new extended UUID;.
Definition: UUID.cpp:114