fw4spl
STexture.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-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 "visuVTKAdaptor/STexture.hpp"
8 
9 #include <fwCom/Signal.hxx>
10 #include <fwCom/Slot.hxx>
11 #include <fwCom/Slots.hxx>
12 
13 #include <fwData/Image.hpp>
14 #include <fwData/Material.hpp>
15 #include <fwData/mt/ObjectReadLock.hpp>
16 #include <fwData/mt/ObjectWriteLock.hpp>
17 #include <fwData/Reconstruction.hpp>
18 
19 #include <fwDataTools/fieldHelper/MedicalImageHelpers.hpp>
20 
21 #include <fwMedData/ModelSeries.hpp>
22 
23 #include <fwServices/macros.hpp>
24 
25 #include <vtkRenderWindowInteractor.h>
26 #include <vtkTexture.h>
27 
28 fwServicesRegisterMacro( ::fwRenderVTK::IAdaptor, ::visuVTKAdaptor::STexture);
29 
30 namespace visuVTKAdaptor
31 {
32 
33 const ::fwCom::Slots::SlotKeyType STexture::s_APPLY_TEXTURE_SLOT = "applyTexture";
34 
35 static const ::fwServices::IService::KeyType s_TEXTURE_INOUT = "texture";
36 
37 //------------------------------------------------------------------------------
38 
39 STexture::STexture() noexcept :
40  m_filtering("linear"),
41  m_wrapping("repeat"),
42  m_lighting(true)
43 {
44  newSlot(s_APPLY_TEXTURE_SLOT, &STexture::applyTexture, this );
45 }
46 
47 //------------------------------------------------------------------------------
48 
50 {
51 }
52 
53 //------------------------------------------------------------------------------
54 
56 {
57  this->configureParams();
58 
59  const ConfigType config = this->getConfigTree().get_child("config.<xmlattr>");
60 
61  m_filtering = config.get<std::string>("filtering", "linear");
62 
63  m_wrapping = config.get<std::string>("wrapping", "repeat");
64 
65  m_lighting = (config.get<std::string>("lighting", "yes") == "yes");
66 }
67 
68 //------------------------------------------------------------------------------
69 
71 {
72  this->initialize();
73 }
74 
75 //------------------------------------------------------------------------------
76 
78 {
79  for(::fwData::Material::sptr material : m_materialSet)
80  {
81  applyTexture(material);
82  }
83 }
84 
85 //------------------------------------------------------------------------------
86 
88 {
89 }
90 
91 //------------------------------------------------------------------------------
92 
94 {
95  if(m_materialSet.count(_material) == 0)
96  {
97  m_materialSet.insert(_material);
98  }
99 
100  ::fwData::Image::sptr image = this->getInOut< ::fwData::Image >(s_TEXTURE_INOUT);
101  SLM_ASSERT("Texture image is missing.", image);
102 
103  ::fwData::mt::ObjectWriteLock matLock(_material);
104 
105  {
106  ::fwData::mt::ObjectReadLock imLock(image);
107 
109  {
110  return;
111  }
112 
113  _material->setDiffuseTexture(image);
114  if(m_lighting == false)
115  {
116  _material->setShadingMode(::fwData::Material::AMBIENT);
117  }
118  }
119 
120  ::fwData::Material::FilteringType filtering = ::fwData::Material::LINEAR;
121  ::fwData::Material::WrappingType wrapping = ::fwData::Material::REPEAT;
122 
123  if(m_filtering == "nearest")
124  {
125  filtering = ::fwData::Material::NEAREST;
126  }
127  else if(m_filtering == "linear")
128  {
129  filtering = ::fwData::Material::LINEAR;
130  }
131  else
132  {
133  OSLM_WARN("STexture filtering type unknown or not supported : " << m_filtering);
134  }
135  _material->setDiffuseTextureFiltering(filtering);
136 
137  if(m_wrapping == "repeat")
138  {
139  wrapping = ::fwData::Material::REPEAT;
140  }
141  else if(m_wrapping == "clamp")
142  {
143  wrapping = ::fwData::Material::CLAMP;
144  }
145  else
146  {
147  OSLM_WARN("STexture wrapping type unknown or not supported : " << m_wrapping);
148  }
149  _material->setDiffuseTextureWrapping(wrapping);
150 
151  ::fwData::Object::ModifiedSignalType::sptr sig;
153  {
154  ::fwCom::Connection::Blocker block(sig->getConnection(m_slotUpdate));
155  sig->asyncEmit();
156  }
157 }
158 
159 //------------------------------------------------------------------------------
160 
162 {
163  KeyConnectionsMap connections;
164  connections.push(s_TEXTURE_INOUT, ::fwData::Image::s_MODIFIED_SIG, s_UPDATE_SLOT);
165  connections.push(s_TEXTURE_INOUT, ::fwData::Image::s_BUFFER_MODIFIED_SIG, s_UPDATE_SLOT);
166 
167  return connections;
168 }
169 
170 //------------------------------------------------------------------------------
171 
172 } //namespace visuVTKAdaptor
VISUVTKADAPTOR_API void stopping() override
Uninitialize the service activity. The stop() method is always invoked before destroying a service...
Definition: STexture.cpp:87
#define SPTR(_cls_)
This class is a helper to define the connections of a service and its data.
Definition: IService.hpp:454
bool m_lighting
enable or not the lighting (default true)
Definition: STexture.hpp:108
VISUVTKADAPTOR_API void configuring() override
Configure the service before starting. Apply the configuration to service.
Definition: STexture.cpp:55
void applyTexture(std::shared_ptr< ::fwData::Material > _material)
Slot called when a texture must be applied on a material.
Definition: STexture.cpp:93
Class allowing to block a Connection.
Definition: Connection.hpp:20
WrappingType
Texture wrapping types.
The namespace visuVTKAdaptor contains the list of adaptors available for the generic scene...
This class defines a material. A material is represented by an ambient color and a diffuse color...
void setShadingMode(ShadingType _shadingMode)
get/set the shading models(flat, gouraud, phong)
std::set< std::shared_ptr< ::fwData::Material > > m_materialSet
Contains all mesh adaptors that currently have this texture applied. Needed when image is updated...
Definition: STexture.hpp:99
FWRENDERVTK_API void configureParams()
Parse the xml configuration for renderer, picker and transform.
A helper to lock object on read mode.
std::string m_wrapping
How to wrap the texture.
Definition: STexture.hpp:105
FilteringType
Texture filtering types.
A helper to lock object on exclusive mode.
VISUVTKADAPTOR_API void starting() override
Initialize the service activity.
Definition: STexture.cpp:70
VISUVTKADAPTOR_API void updating() override
Perform some computations according to object (this service is attached to) attribute values and its ...
Definition: STexture.cpp:77
VISUVTKADAPTOR_API STexture() noexcept
Constructor.
Definition: STexture.cpp:39
void setDiffuseTextureWrapping(WrappingType _diffuseTextureWrapping)
get/set the texture wrapping
UpdateSlotType::sptr m_slotUpdate
Slot to call update method.
Definition: IService.hpp:690
virtual VISUVTKADAPTOR_API KeyConnectionsMap getAutoConnections() const override
Returns proposals to connect service slots to associated object signals, this method is used for obj/...
Definition: STexture.cpp:161
std::string m_filtering
How to filter this texture.
Definition: STexture.hpp:102
FWDATA_API void setDiffuseTexture(const Image::sptr &diffuseTexture)
Setter for diffuse texture.
virtual VISUVTKADAPTOR_API ~STexture() noexcept
Destructor.
Definition: STexture.cpp:49
#define OSLM_WARN(message)
Definition: spyLog.hpp:263
#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 FWDATA_APIconst::fwCom::Signals::SignalKeyType s_BUFFER_MODIFIED_SIG
Type of signal when image&#39;s buffer is added.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_MODIFIED_SIG
Key in m_signals map of signal m_sigModified.
static FWDATATOOLS_API bool checkImageValidity(::fwData::Image::csptr _pImg)
Check if the image is valid.
FWRENDERVTK_API void initialize()
Initialize the adaptor with the associated render service. (must be call in starting).
void setDiffuseTextureFiltering(FilteringType _diffuseTextureFiltering)
get/set the texture filtering
Adaptor to map a texture on a mesh.
Definition: STexture.hpp:59
static FWSERVICES_APIconst::fwCom::Slots::SlotKeyType s_UPDATE_SLOT
Slot to call start method.
Definition: IService.hpp:177
FWSERVICES_API ConfigType getConfigTree() const
Return the configuration, in an boost property tree.
Definition: IService.cpp:247