fw4spl
patch/fwMDSemanticPatch/src/fwMDSemanticPatch/V10/V09ALA/fwData/Image.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 "fwMDSemanticPatch/V10/V09ALA/fwData/Image.hpp"
8 
9 #include <fwAtoms/Boolean.hpp>
10 #include <fwAtoms/Map.hpp>
11 #include <fwAtoms/Numeric.hpp>
12 #include <fwAtoms/Numeric.hxx>
13 #include <fwAtoms/Object.hpp>
14 #include <fwAtoms/Object.hxx>
15 #include <fwAtoms/String.hpp>
16 
17 #include <fwAtomsPatch/helper/functions.hpp>
18 #include <fwAtomsPatch/StructuralCreatorDB.hpp>
19 
20 #include <fwTools/UUID.hpp>
21 
22 #include <boost/algorithm/string.hpp>
23 
24 namespace fwMDSemanticPatch
25 {
26 namespace V10
27 {
28 namespace V09ALA
29 {
30 namespace fwData
31 {
32 
34  ::fwAtomsPatch::ISemanticPatch()
35 {
36  m_originClassname = "::fwData::Image";
37  m_originVersion = "2";
38  this->addContext("MedicalData", "V10", "V09ALA");
39 }
40 
41 // ----------------------------------------------------------------------------
42 
44 {
45 }
46 
47 // ----------------------------------------------------------------------------
48 
49 Image::Image( const Image& cpy ) :
51 {
52 }
53 
54 // ----------------------------------------------------------------------------
55 
57  const ::fwAtoms::Object::sptr& previous,
58  const ::fwAtoms::Object::sptr& current,
59  ::fwAtomsPatch::IPatch::NewVersionsType& newVersions)
60 {
61  ISemanticPatch::apply(previous, current, newVersions);
62  ::fwAtomsPatch::helper::cleanFields( current );
63  ::fwAtomsPatch::helper::Object helper( current );
64 
65  ::fwAtoms::Map::csptr previousFieldMap = ::fwAtoms::Map::dynamicCast(previous->getAttribute("fields"));
66  SLM_ASSERT("Image does not have field map", previousFieldMap);
67 
68  const auto& iter = previousFieldMap->find("m_landmarksId");
69  if (iter != previousFieldMap->end())
70  {
71  // create new Landmarks structure
72  ::fwAtomsPatch::StructuralCreatorDB::sptr creators = ::fwAtomsPatch::StructuralCreatorDB::getDefault();
73 
74  ::fwAtoms::Object::sptr currentPL = creators->create( "::fwData::PointList", "1");
75  ::fwAtomsPatch::helper::Object helperPL( currentPL );
76 
77  ::fwAtoms::Map::sptr currentFieldMap = ::fwAtoms::Map::dynamicCast(current->getAttribute("fields"));
78  currentFieldMap->insert("m_imageLandmarksId", currentPL);
79 
80  ::fwAtoms::Sequence::sptr plSeq = ::fwAtoms::Sequence::dynamicCast(currentPL->getAttribute("points"));
81 
82  // Convert previous Landmarks
83  ::fwAtoms::Object::sptr previousLandmarks = ::fwAtoms::Object::dynamicCast(iter->second);
84 
85  ::fwAtoms::Map::sptr previousLandmarksMap =
86  ::fwAtoms::Map::dynamicCast(previousLandmarks->getAttribute("landmarks"));
87 
88  for (const auto& elt : previousLandmarksMap->getValue())
89  {
90  ::fwAtoms::Object::csptr atomGroup = ::fwAtoms::Object::dynamicCast(elt.second);
91  ::fwAtoms::Sequence::csptr points = ::fwAtoms::Sequence::dynamicCast(atomGroup->getAttribute("points"));
92 
93  size_t count = 0;
94  for (const auto& ptObj : points->getValue())
95  {
96  ::fwAtoms::String::csptr previousPt = ::fwAtoms::String::dynamicCast(ptObj);
97  std::vector<std::string> res;
98  const std::string coords = previousPt->getValue();
99  ::boost::split(res, coords, ::boost::is_any_of(";"));
100 
101  ::fwAtoms::Object::sptr point = ::fwAtoms::Object::New();
102  ::fwAtomsPatch::helper::setClassname(point, "::fwData::Point");
103  ::fwAtomsPatch::helper::setVersion(point, "1");
104  ::fwAtomsPatch::helper::generateID(point);
105 
106  plSeq->push_back(point);
107 
108  ::fwAtomsPatch::helper::Object helperPt( point );
109  ::fwAtoms::Sequence::sptr pointCoord = ::fwAtoms::Sequence::New();
110  helperPt.addAttribute("coord", pointCoord);
111  pointCoord->push_back(::fwAtoms::Numeric::New(std::stod(res[0])));
112  pointCoord->push_back(::fwAtoms::Numeric::New(std::stod(res[1])));
113  pointCoord->push_back(::fwAtoms::Numeric::New(std::stod(res[2])));
114 
115  // set point label
116  ::fwAtoms::Map::sptr pointFieldMap = ::fwAtoms::Map::New();
117  helperPt.addAttribute("fields", pointFieldMap);
118 
119  const std::string label = elt.first + "_" + std::to_string(count);
120 
121  pointFieldMap->insert("m_labelId", ::fwAtoms::String::New(label));
122  ++count;
123  }
124  }
125  }
126 }
127 
128 // ----------------------------------------------------------------------------
129 
130 } // namespace fwData
131 } // namespace V09ALA
132 } // namespace V10
133 } // namespace fwMDSemanticPatch
134 
Patch the &#39;landmarks&#39; field of an image from version &#39;V10&#39; to &#39;V09ALA&#39; within &#39;MedicalData&#39; context...
Contains the semantic patches within the context "MedicalData".
virtual FWMDSEMANTICPATCH_API void apply(const ::fwAtoms::Object::sptr &previous, const ::fwAtoms::Object::sptr &current,::fwAtomsPatch::IPatch::NewVersionsType &newVersions) override
Applies patch.
static FWATOMS_API String::sptr New(std::string value)
Construct a new Object represented a string.
Contains base functionalities used to transform objects from a version to another.
Definition: Abstract.hpp:16
std::string m_originClassname
Origin classname of the object.
Definition: IPatch.hpp:64
virtual FWATOMSPATCH_API void addContext(const std::string &context, const std::string &originVersion, const std::string &targetVersion)
Add a context where the patch can be applied.
std::string m_originVersion
Origin version of the object.
Definition: IPatch.hpp:67
FWATOMSPATCH_API ISemanticPatch()
Constructor.
static Numeric::sptr New(T value)
Build a new numeric type.
Definition: Numeric.hxx:77
#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
FWATOMSPATCH_API void addAttribute(const std::string &name,::fwAtoms::Base::sptr value, conditions::Abstract::sptr condition=conditions::Abstract::New())
Adds a new attribute in the current object.
static StructuralCreatorDB::sptr getDefault()
Returns the default instance of StructuralCreatorDB.
Contains the representation of the data objects used in the framework.