Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkSimulationIO.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
18 #include "mitkISimulationService.h"
19 #include "mitkSimulation.h"
20 #include "mitkSimulationIO.h"
21 #include <mitkCustomMimeType.h>
22 #include <mitkStringProperty.h>
23 #include <sofa/helper/system/SetDirectory.h>
24 #include <algorithm>
25 
26 #include <mitkIOMimeTypes.h>
27 
29 {
30  mitk::CustomMimeType mimeType(mitk::IOMimeTypes::DEFAULT_BASE_NAME() + ".simulation.sofa.scene");
31  mimeType.SetCategory("Simulation");
32  mimeType.AddExtension("scn");
33  mimeType.AddExtension("xml");
34  mimeType.SetComment("SOFA Scene File");
35 
36  return mimeType;
37 }
38 
41 {
42  this->RegisterService();
43 }
44 
45 std::vector<mitk::BaseData::Pointer> mitk::SimulationIO::Read()
46 {
47  Simulation::Pointer simulation = Simulation::New();
48  sofa::simulation::Simulation::SPtr sofaSimulation = simulation->GetSOFASimulation();
49 
50  ISimulationService* simulationService = GetSimulationService();
51  Simulation::Pointer lastActiveSimulation = simulationService->GetActiveSimulation();
52 
53  simulationService->SetActiveSimulation(simulation);
54 
55  std::ifstream scnFile(this->GetLocalFileName().c_str());
56  std::string content = std::string((std::istreambuf_iterator<char>(scnFile)), std::istreambuf_iterator<char>());
57  scnFile.close();
58 
59  std::istringstream stream(content);
60  std::string firstLine;
61 
62  if (!std::getline(stream, firstLine).good())
63  mitkThrow() << "Could not load '" << this->GetLocalFileName() << "'!";
64 
65  std::string originalPath;
66 
67  if (firstLine.size() > 21 && firstLine.substr(0, 21) == "<!-- ORIGINAL_PATH = ")
68  {
69  originalPath = firstLine.substr(21);
70  sofa::helper::system::DataRepository.addFirstPath(originalPath);
71  }
72 
73  std::string path = sofa::helper::system::SetDirectory::GetParentDir(this->GetLocalFileName().c_str());
74  sofa::helper::system::DataRepository.addFirstPath(path);
75 
76  sofa::simulation::Node::SPtr rootNode = sofa::core::objectmodel::SPtr_dynamic_cast<sofa::simulation::Node>(sofaSimulation->load(this->GetLocalFileName().c_str()));
77 
78  if (!rootNode)
79  {
80  sofa::helper::system::DataRepository.removePath(path);
81  mitkThrow() << "Could not load '" << this->GetLocalFileName() << "'!";
82  }
83 
84  simulation->SetRootNode(rootNode);
85 
86  sofaSimulation->init(rootNode.get());
87  sofaSimulation->reset(rootNode.get());
88  simulation->UpdateOutputInformation();
89 
90  sofa::helper::system::DataRepository.removePath(path);
91 
92  if (!originalPath.empty())
93  {
94  sofa::helper::system::DataRepository.removePath(originalPath);
95  simulation->SetProperty("Path", StringProperty::New(originalPath));
96  }
97  else
98  {
99  simulation->SetProperty("Path", StringProperty::New(path));
100  }
101 
102  simulation->SetProperty("Scene File", StringProperty::New(content));
103 
104  simulationService->SetActiveSimulation(lastActiveSimulation);
105 
106  std::vector<BaseData::Pointer> returnValue;
107  returnValue.push_back(simulation.GetPointer());
108 
109  return returnValue;
110 }
111 
113 {
114  if (AbstractFileIO::GetReaderConfidenceLevel() == Unsupported)
115  return Unsupported;
116 
117  std::string inputLocation = this->GetInputLocation();
118  std::string::size_type length = inputLocation.length();
119 
120  if (length < 5)
121  return Unsupported;
122 
123  std::string ext = inputLocation.substr(length - 4);
124  std::transform(ext.begin(), ext.end(), ext.begin(), tolower);
125 
126  return ext != ".scn" && ext != ".xml"
127  ? Unsupported
128  : Supported;
129 }
130 
132 {
133  this->ValidateOutputLocation();
134 
135  const Simulation* simulation = dynamic_cast<const Simulation*>(this->GetInput());
136 
137  if (simulation == NULL)
138  mitkThrow() << "Invalid input!";
139 
140  std::string originalContent = simulation->GetProperty("Scene File")->GetValueAsString();
141 
142  LocalFile localFile(this);
143  std::ofstream scnFile(localFile.GetFileName().c_str());
144 
145  if (originalContent.size() < 22 || originalContent.substr(0, 21) != "<!-- ORIGINAL_PATH = ")
146  {
147  std::string originalPath = "<!-- ORIGINAL_PATH = ";
148  originalPath += simulation->GetProperty("Path")->GetValueAsString();
149  originalPath += "\n This comment is parsed by MITK. -->\n";
150 
151  scnFile << originalPath;
152  }
153 
154  scnFile << originalContent;
155 }
156 
158 {
159  if (AbstractFileIO::GetWriterConfidenceLevel() == Unsupported)
160  return Unsupported;
161 
162  return dynamic_cast<const Simulation*>(this->GetInput()) == NULL
163  ? Unsupported
164  : Supported;
165 }
166 
167 mitk::SimulationIO* mitk::SimulationIO::IOClone() const
168 {
169  return new SimulationIO(*this);
170 }
std::vector< BaseData::Pointer > Read() override
Reads a path or stream and creates a list of BaseData objects.
virtual ConfidenceLevel GetReaderConfidenceLevel() const override
void SetComment(const std::string &comment)
ConfidenceLevel GetWriterConfidenceLevel() const override
The CustomMimeType class represents a custom mime-type which may be registered as a service object...
static std::string DEFAULT_BASE_NAME()
std::pair< us::ServiceRegistration< IFileReader >, us::ServiceRegistration< IFileWriter > > RegisterService(us::ModuleContext *context=us::GetModuleContext())
static Pointer New()
#define mitkThrow()
MITKSIMULATION_EXPORT ISimulationService * GetSimulationService(us::ModuleContext *moduleContext=us::GetModuleContext())
static const char * GetStaticNameOfClass()
ConfidenceLevel GetReaderConfidenceLevel() const override
A local file representation for streams.
virtual void SetActiveSimulation(Simulation::Pointer activeSimulation)=0
void AddExtension(const std::string &extension)
void SetCategory(const std::string &category)
static mitk::CustomMimeType CreateSimulationMimeType()
ConfidenceLevel
A confidence level describing the confidence of the reader or writer in handling the given data...
Definition: mitkIFileIO.h:49
virtual ConfidenceLevel GetWriterConfidenceLevel() const override
mitk::BaseProperty::Pointer GetProperty(const char *propertyKey) const
Get the property (instance of BaseProperty) with key propertyKey from the PropertyList, and set it to this, respectively;.
Abstract class for implementing a reader and writer.
virtual Simulation::Pointer GetActiveSimulation() const =0
static Pointer New()
void Write() override
Write the base data to the specified location or output stream.