fw4spl
patch/fwMDSemanticPatch/src/fwMDSemanticPatch/V09ALA/V10/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/V09ALA/V10/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 namespace fwMDSemanticPatch
23 {
24 namespace V09ALA
25 {
26 namespace V10
27 {
28 namespace fwData
29 {
30 
32  ::fwAtomsPatch::ISemanticPatch()
33 {
34  m_originClassname = "::fwData::Image";
35  m_originVersion = "2";
36  this->addContext("MedicalData", "V09ALA", "V10");
37 }
38 
39 // ----------------------------------------------------------------------------
40 
42 {
43 }
44 
45 // ----------------------------------------------------------------------------
46 
47 Image::Image( const Image& cpy ) :
49 {
50 }
51 
52 // ----------------------------------------------------------------------------
53 
55  const ::fwAtoms::Object::sptr& previous,
56  const ::fwAtoms::Object::sptr& current,
57  ::fwAtomsPatch::IPatch::NewVersionsType& newVersions)
58 {
59  ISemanticPatch::apply(previous, current, newVersions);
60  ::fwAtomsPatch::helper::cleanFields( current );
61  ::fwAtomsPatch::helper::Object helper( current );
62 
63  ::fwAtoms::Map::csptr previousFieldMap = ::fwAtoms::Map::dynamicCast(previous->getAttribute("fields"));
64  SLM_ASSERT("Image does not have field map", previousFieldMap);
65 
66  const auto& iter = previousFieldMap->find("m_imageLandmarksId");
67  if (iter != previousFieldMap->end())
68  {
69  // create new Landmarks structure
70  ::fwAtomsPatch::StructuralCreatorDB::sptr creators = ::fwAtomsPatch::StructuralCreatorDB::getDefault();
71 
72  ::fwAtoms::Object::sptr currentLandmarks = creators->create( "::fwData::Landmarks", "1");
73  ::fwAtomsPatch::helper::Object helperLandmarks( currentLandmarks );
74 
75  ::fwAtoms::Map::sptr currentFieldMap = ::fwAtoms::Map::dynamicCast(current->getAttribute("fields"));
76  currentFieldMap->insert("m_landmarksId", currentLandmarks);
77 
78  ::fwAtoms::Map::sptr landmarksMap = ::fwAtoms::Map::dynamicCast(currentLandmarks->getAttribute("landmarks"));
79 
80  // Convert previous PointList
81  ::fwAtoms::Object::sptr previousPL = ::fwAtoms::Object::dynamicCast(iter->second);
82 
83  ::fwAtoms::Sequence::sptr previousPLSeq = ::fwAtoms::Sequence::dynamicCast(previousPL->getAttribute("points"));
84 
85  size_t count = 0;
86  for (const auto& obj : previousPLSeq->getValue())
87  {
88  // get point coordinates
89  ::fwAtoms::Object::csptr point = ::fwAtoms::Object::dynamicCast(obj);
90  ::fwAtoms::Sequence::csptr pointCoords = ::fwAtoms::Sequence::dynamicCast(point->getAttribute("coord"));
91  ::fwAtoms::Numeric::csptr coordX = ::fwAtoms::Numeric::dynamicCast(pointCoords->getValue()[0]);
92  ::fwAtoms::Numeric::csptr coordY = ::fwAtoms::Numeric::dynamicCast(pointCoords->getValue()[1]);
93  ::fwAtoms::Numeric::csptr coordZ = ::fwAtoms::Numeric::dynamicCast(pointCoords->getValue()[2]);
94  const std::string coords = coordX->getString() + ";" + coordY->getString() + ";" + coordZ->getString();
95 
96  // get point label
97  ::fwAtoms::Map::csptr pointFieldMap = ::fwAtoms::Map::dynamicCast(point->getAttribute("fields"));
98 
99  std::string label;
100  const auto& it = pointFieldMap->find("m_labelId");
101  if (it != pointFieldMap->end())
102  {
103  ::fwAtoms::Object::csptr labelObj = ::fwAtoms::Object::dynamicCast(it->second);
104  ::fwAtoms::String::csptr labelStr = ::fwAtoms::String::dynamicCast(labelObj->getAttribute("value"));
105  label = labelStr->getValue();
106  }
107  if (label.empty())
108  {
109  label = "label_" + std::to_string(count++);
110  }
111 
112  // create one landmark group per point
113  ::fwAtoms::Object::sptr atomGroup = ::fwAtoms::Object::New();
114  atomGroup->setMetaInfo("ID_METAINFO", ::fwTools::UUID::generateUUID());
115 
116  atomGroup->setAttribute("color", ::fwAtoms::String::New("1;1;1;1"));
117  atomGroup->setAttribute("size", ::fwAtoms::Numeric::New(1));
118  atomGroup->setAttribute("shape", ::fwAtoms::String::New("SPHERE"));
119  atomGroup->setAttribute("visibility", ::fwAtoms::Boolean::New(true));
120 
121  ::fwAtoms::Sequence::sptr seq = ::fwAtoms::Sequence::New();
122 
123  seq->push_back(::fwAtoms::String::New(coords));
124  atomGroup->setAttribute("points", seq);
125  landmarksMap->insert(label, atomGroup);
126  }
127  }
128 }
129 
130 // ----------------------------------------------------------------------------
131 
132 } // namespace fwData
133 } // namespace V10
134 } // namespace V09ALA
135 } // namespace fwMDSemanticPatch
136 
Contains the semantic patches within the context "MedicalData".
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
static StructuralCreatorDB::sptr getDefault()
Returns the default instance of StructuralCreatorDB.
Patch the &#39;landmarks&#39; field of an image from version &#39;V9ALA&#39; to &#39;V10&#39; within &#39;MedicalData&#39; context...
Contains the representation of the data objects used in the framework.
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 Boolean::sptr New(std::string value)
Construct an object storing a bool value.
static FWTOOLS_API UUIDType generateUUID()
Return a new extended UUID;.
Definition: UUID.cpp:114