fw4spl
V3ToV4.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2015-2016.
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 "fwStructuralPatch/fwData/Material/V3ToV4.hpp"
8 
9 #include <fwAtoms/Boolean.hpp>
10 #include <fwAtoms/Numeric.hpp>
11 #include <fwAtoms/Numeric.hxx>
12 #include <fwAtoms/Object.hpp>
13 #include <fwAtoms/Object.hxx>
14 #include <fwAtoms/Sequence.hpp>
15 #include <fwAtoms/String.hpp>
16 #include <fwAtomsPatch/StructuralCreatorDB.hpp>
17 
18 namespace fwStructuralPatch
19 {
20 
21 namespace fwData
22 {
23 
24 namespace Material
25 {
26 
27 V3ToV4::V3ToV4() : ::fwAtomsPatch::IStructuralPatch()
28 {
29  m_originClassname = "::fwData::Material";
30  m_targetClassname = "::fwData::Material";
31  m_originVersion = "3";
32  m_targetVersion = "4";
33 }
34 
35 // ----------------------------------------------------------------------------
36 
38 {
39 }
40 
41 // ----------------------------------------------------------------------------
42 
44 {
45 }
46 
47 // ----------------------------------------------------------------------------
48 
49 void V3ToV4::apply( const ::fwAtoms::Object::sptr& previous,
50  const ::fwAtoms::Object::sptr& current,
51  ::fwAtomsPatch::IPatch::NewVersionsType& newVersions)
52 {
53  IStructuralPatch::apply(previous, current, newVersions);
54 
55  // Update object version
56  this->updateVersion(current);
57 
58  ::fwAtomsPatch::helper::Object helper(current);
59 
60  // Retrieve and remove lighting attribute, then update shading mode
61  ::fwAtoms::Boolean::sptr lighting = previous->getAttribute< ::fwAtoms::Boolean >("lighting");
62  bool bLighting = lighting->getValue();
63 
64  helper.removeAttribute("lighting");
65 
66  if(!bLighting)
67  {
68  helper.replaceAttribute("shading_mode", ::fwAtoms::String::New("AMBIENT"));
69  }
70 
71  // Switch diffuse and ambient
72  ::fwAtoms::Object::sptr ambient = current->getAttribute< ::fwAtoms::Object >("ambient");
73  ::fwAtoms::Object::sptr diffuse = current->getAttribute< ::fwAtoms::Object >("diffuse");
74 
75  // Replace diffuse by previous ambient
76  helper.replaceAttribute("diffuse", ambient);
77  helper.replaceAttribute("ambient", diffuse);
78 
79  // Set ambient to 0.05f
80 
81  ::fwAtoms::Sequence::sptr ambientSeq = diffuse->getAttribute< ::fwAtoms::Sequence >("rgba");
82  ::fwAtoms::Numeric::sptr r = std::dynamic_pointer_cast< ::fwAtoms::Numeric >((*ambientSeq)[0]);
83  r->setFromString(".05");
84  ::fwAtoms::Numeric::sptr g = std::dynamic_pointer_cast< ::fwAtoms::Numeric >((*ambientSeq)[1]);
85  g->setFromString(".05");
86  ::fwAtoms::Numeric::sptr b = std::dynamic_pointer_cast< ::fwAtoms::Numeric >((*ambientSeq)[2]);
87  b->setFromString(".05");
88  ::fwAtoms::Numeric::sptr a = std::dynamic_pointer_cast< ::fwAtoms::Numeric >((*ambientSeq)[3]);
89  a->setFromString("1.");
90 }
91 
92 } // namespace Material
93 
94 } // namespace fwData
95 
96 } // namespace fwStructuralPatch
FWATOMS_API void setFromString(const std::string &s, NumericType type=EMPTY)
Sets Numeric&#39;s value from given string, using Numeric::valueFromString.
Definition: Numeric.cpp:102
Structural patch to convert a fwData::Material from version &#39;3&#39; to version &#39;4&#39;.
Definition: V3ToV4.hpp:24
FWSTRUCTURALPATCH_API V3ToV4()
Constructor.
Definition: V3ToV4.cpp:27
FWATOMSPATCH_API void updateVersion(std::shared_ptr< ::fwAtoms::Object > current)
Updates the version of the patched object.
static FWATOMS_API String::sptr New(std::string value)
Construct a new Object represented a string.
FWATOMS_API Base::sptr getAttribute(const std::string &key) const
Returns requested attribute if exists, empty sptr else.
FWSTRUCTURALPATCH_API ~V3ToV4()
Destructor.
Definition: V3ToV4.cpp:37
FWATOMSPATCH_API void removeAttribute(const std::string &name, conditions::Abstract::sptr condition=conditions::Abstract::New())
Removes an attribute.
Contains base functionalities used to transform objects from a version to another.
Definition: Abstract.hpp:16
Class represented a fwData::Object.
std::string m_targetClassname
Target type.
FWATOMSPATCH_API IStructuralPatch()
Constructor.
std::string m_originClassname
Origin classname of the object.
Definition: IPatch.hpp:64
Contains creators and structural patches.
Definition: Landmarks1.hpp:13
FWATOMSPATCH_API void replaceAttribute(const std::string &name,::fwAtoms::Base::sptr newValue, conditions::Abstract::sptr condition=conditions::Abstract::New())
Replaces an attribute.
std::string m_originVersion
Origin version of the object.
Definition: IPatch.hpp:67
Represented a Boolean value.
std::string m_targetVersion
Target version.
virtual FWSTRUCTURALPATCH_API void apply(const ::fwAtoms::Object::sptr &previous, const ::fwAtoms::Object::sptr &current,::fwAtomsPatch::IPatch::NewVersionsType &newVersions) override
Applies patch.
Definition: V3ToV4.cpp:49
Storing a numeric value.
Definition: Numeric.hpp:25
Contains the representation of the data objects used in the framework.
Sequence represented a list of meta object.
Definition: Sequence.hpp:22