fw4spl
SExtractMeshByType.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2018.
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 "ctrlCamp/SExtractMeshByType.hpp"
8 
9 #include <fwData/Mesh.hpp>
10 #include <fwData/mt/ObjectWriteLock.hpp>
11 #include <fwData/Reconstruction.hpp>
12 
13 #include <fwMedData/ModelSeries.hpp>
14 
15 #include <fwRuntime/ConfigurationElement.hpp>
16 
17 #include <fwServices/registry/ObjectService.hpp>
18 
19 #include <boost/algorithm/string.hpp>
20 #include <boost/regex.hpp>
21 
22 namespace ctrlCamp
23 {
24 
25 fwServicesRegisterMacro(::ctrlCamp::ICamp, ::ctrlCamp::SExtractMeshByType);
26 
27 //-----------------------------------------------------------------------------
28 
30 {
31 }
32 
33 //-----------------------------------------------------------------------------
34 
36 {
37 }
38 
39 //-----------------------------------------------------------------------------
40 
42 {
43  typedef ::fwRuntime::ConfigurationElement::sptr ConfigType;
44 
45  const ConfigType inoutCfg = m_configuration->findConfigurationElement("inout");
46  SLM_ASSERT("At one 'inout' tag is required.", inoutCfg);
47 
48  const std::vector< ConfigType > extractCfg = inoutCfg->find("extract");
49  SLM_ASSERT("At least one 'extract' tag is required.", !extractCfg.empty());
50 
51  bool ok = false;
52 
53  const std::vector< ConfigType > outCfg = m_configuration->find("out");
54  for (const auto& cfg : outCfg)
55  {
56  if(cfg->hasAttribute("group"))
57  {
58  if(cfg->getAttributeValue("group") == "target")
59  {
60  const std::vector< ConfigType > keyCfg = cfg->find("key");
62  "You must have as many 'extract' tags as 'out' keys." << extractCfg.size() << " " << keyCfg.size(),
63  extractCfg.size() == keyCfg.size());
64  ok = true;
65  }
66  }
67  }
68  SLM_ASSERT("Missing 'target' output keys", ok);
69 
70  for(ConfigType cfg : extractCfg)
71  {
72  const std::string type = cfg->getAttributeValue("type");
73  const std::string regex = cfg->getAttributeValue("matching");
74 
75  m_extract.push_back(std::make_pair(type, regex));
76  }
77 }
78 
79 //-----------------------------------------------------------------------------
80 
82 {
83 }
84 
85 //-----------------------------------------------------------------------------
86 
88 {
89  ::fwMedData::ModelSeries::sptr modelSeries = this->getInOut< ::fwMedData::ModelSeries>("source");
90  OSLM_ASSERT("ModelSeries not found", modelSeries);
91 
92  size_t index = 0;
93  for(const auto& elt : m_extract)
94  {
95  const std::string type = elt.first;
96  const std::string regex = elt.second;
97 
98  bool found = false;
99  const ::fwMedData::ModelSeries::ReconstructionVectorType recs = modelSeries->getReconstructionDB();
100  for(const ::fwData::Reconstruction::csptr element : recs)
101  {
102  if(element->getStructureType() == type)
103  {
104  const ::boost::regex regSurface(regex);
105  ::boost::smatch match;
106  const std::string name = element->getOrganName();
107 
108  if(regex.empty() || ::boost::regex_match(name, match, regSurface))
109  {
110  ::fwData::Mesh::sptr obj = element->getMesh();
111 
112  this->setOutput("target", obj, index);
113  found = true;
114  ++index;
115 
116  break;
117  }
118  }
119  }
121  "Mesh with organ name matching '" << regex << "' and structure type'" << type << "' didn't find",
122  !found);
123  }
124 }
125 
126 //-----------------------------------------------------------------------------
127 
129 {
130  // Unregister outputs
131  for (size_t i = 0; i < this->getKeyGroupSize("target"); ++i)
132  {
133  this->setOutput("target", nullptr, i);
134  }
135 }
136 
137 //-----------------------------------------------------------------------------
138 
139 } // namespace ctrlCamp
#define OSLM_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:310
virtual CTRLCAMP_API void updating() override
Does nothing.
The namespace ctrlCamp contains services using camp.
Definition: ICamp.hpp:18
CTRLCAMP_API ~SExtractMeshByType()
Destructor.
FWSERVICES_API void setOutput(const ::fwServices::IService::KeyType &key, const ::fwData::Object::sptr &object, size_t index=0)
Register an output object at a given key in the OSR, replacing it if it already exists.
Definition: IService.cpp:80
This interface defines service API. It can be considered as a default type for services using fwCamp...
Definition: ICamp.hpp:23
#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
::fwRuntime::ConfigurationElement::sptr m_configuration
Configuration element used to configure service internal state using a generic XML like structure TOD...
Definition: IService.hpp:670
This service extract meshes of a specific structure type from fwMedData::ModelSeries. If the model series have some meshes with the same structure type, it will extract the first found. Additionnaly, it can also match the organ name with a regular expression.
size_t getKeyGroupSize(const KeyType &keybase) const
Return the number of key in a group of keys.
Definition: IService.hxx:119
CTRLCAMP_API SExtractMeshByType()
Constructor.
virtual CTRLCAMP_API void starting() override
Does nothing.
virtual CTRLCAMP_API void configuring() override
Configure the service.
#define OSLM_ERROR_IF(message, cond)
Definition: spyLog.hpp:278
virtual CTRLCAMP_API void stopping() override
Does nothing.