Medical Imaging Interaction Toolkit  2018.4.99-a3d2e8fb
Medical Imaging Interaction Toolkit
mitkPAIOUtil.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 (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #include "mitkPAIOUtil.h"
14 
15 #include "mitkIOUtil.h"
16 #include "mitkImageReadAccessor.h"
17 
18 #include <string>
19 #include <sstream>
20 #include <vector>
21 
22 #include "mitkPAComposedVolume.h"
24 #include "mitkPANoiseGenerator.h"
26 #include <mitkProperties.h>
27 #include <itkDirectory.h>
28 #include <itksys/SystemTools.hxx>
29 
30 static std::vector<int> splitString(const std::string &s, const char* delim) {
31  std::vector<int> elems;
32  std::stringstream ss(s);
33  std::string item;
34  while (std::getline(ss, item, *delim))
35  {
36  int numb;
37  std::stringstream(item) >> numb;
38  elems.push_back(numb);
39  }
40  return elems;
41 }
42 
43 bool mitk::pa::IOUtil::DoesFileHaveEnding(std::string const &fullString, std::string const &ending) {
44  if (fullString.length() == 0 || ending.length() == 0 || fullString.length() < ending.length())
45  return false;
46 
47  return (0 == fullString.compare(fullString.length() - ending.length(), ending.length(), ending));
48 }
49 
50 mitk::pa::IOUtil::IOUtil() {}
51 
52 mitk::pa::IOUtil::~IOUtil() {}
53 
54 mitk::pa::Volume::Pointer mitk::pa::IOUtil::LoadNrrd(std::string filename, double blur)
55 {
56  if (filename.empty() || filename == "")
57  return nullptr;
58 
59  mitk::Image::Pointer inputImage = mitk::IOUtil::Load<mitk::Image>(filename);
60 
61  if (inputImage.IsNull())
62  return nullptr;
63 
64  auto returnImage = Volume::New(inputImage);
65 
66  VolumeManipulator::GaussianBlur3D(returnImage, blur);
67 
68  return returnImage;
69 }
70 
71 std::map<mitk::pa::IOUtil::Position, mitk::pa::Volume::Pointer>
72 mitk::pa::IOUtil::LoadFluenceContributionMaps(std::string foldername, double blur, int* progress, bool doLog10)
73 {
74  std::map<IOUtil::Position, Volume::Pointer> resultMap;
75 
76  itk::Directory::Pointer directoryHandler = itk::Directory::New();
77  directoryHandler->Load(foldername.c_str());
78  for (unsigned int fileIndex = 0, numFiles = directoryHandler->GetNumberOfFiles(); fileIndex < numFiles; ++fileIndex)
79  {
80  std::string filename = std::string(directoryHandler->GetFile(fileIndex));
81  if (itksys::SystemTools::FileIsDirectory(filename))
82  continue;
83 
84  if (!DoesFileHaveEnding(filename, ".nrrd"))
85  continue;
86 
87  size_t s = filename.find("_p");
88  size_t e = filename.find("Fluence", s);
89  std::string sub = filename.substr(s + 2, e - s - 2);
90  std::vector<int> coords = splitString(sub, ",");
91 
92  if (coords.size() != 3)
93  {
94  MITK_ERROR << "Some of the data to read was corrupted or did not match the " <<
95  "naming pattern *_pN,N,NFluence*.nrrd";
96  mitkThrow() << "Some of the data to read was corrupted or did not match the" <<
97  " naming pattern *_pN,N,NFluence*.nrrd";
98  }
99  else
100  {
101  MITK_DEBUG << "Extracted coords: " << coords[0] << "|" << coords[1] << "|" << coords[2] << " from string " << sub;
102  Volume::Pointer nrrdFile = LoadNrrd(foldername + filename, blur);
103  if (doLog10)
105  resultMap[Position{ coords[0], coords[2] }] = nrrdFile;
106  *progress = *progress + 1;
107  }
108  }
109 
110  return resultMap;
111 }
112 
114 {
115  return GetListOfAllNrrdFilesInDirectory(directory).size();
116 }
117 
118 std::vector<std::string> mitk::pa::IOUtil::GetListOfAllNrrdFilesInDirectory(std::string directory, bool keepFileFormat)
119 {
120  std::vector<std::string> filenames;
121  itk::Directory::Pointer directoryHandler = itk::Directory::New();
122  directoryHandler->Load(directory.c_str());
123  for (unsigned int fileIndex = 0, numFiles = directoryHandler->GetNumberOfFiles(); fileIndex < numFiles; ++fileIndex)
124  {
125  std::string filename = std::string(directoryHandler->GetFile(fileIndex));
126  if (itksys::SystemTools::FileIsDirectory(filename))
127  continue;
128 
129  if (!DoesFileHaveEnding(filename, ".nrrd"))
130  continue;
131 
132  if (keepFileFormat)
133  {
134  filenames.push_back(filename);
135  }
136  else
137  {
138  filenames.push_back(filename.substr(0, filename.size() - 5));
139  }
140  }
141 
142  return filenames;
143 }
144 
145 std::vector<std::string> mitk::pa::IOUtil::GetAllChildfoldersFromFolder(std::string folderPath)
146 {
147  std::vector<std::string> returnVector;
148 
149  itksys::Directory directoryHandler;
150  directoryHandler.Load(folderPath.c_str());
151  for (unsigned int fileIndex = 0, numFiles = directoryHandler.GetNumberOfFiles(); fileIndex < numFiles; ++fileIndex)
152  {
153  std::string foldername = std::string(directoryHandler.GetFile(fileIndex));
154  std::string filename = folderPath + "/" + foldername;
155  if (itksys::SystemTools::FileIsDirectory(filename))
156  {
157  if (foldername != std::string(".") && foldername != std::string(".."))
158  {
159  MITK_INFO << filename;
160  returnVector.push_back(filename);
161  }
162  continue;
163  }
164 
165  //If there is a nrrd file in the directory we assume that a bottom level directory was chosen.
166  if (DoesFileHaveEnding(filename, ".nrrd"))
167  {
168  returnVector.clear();
169  returnVector.push_back(folderPath);
170  return returnVector;
171  }
172  }
173 
174  return returnVector;
175 }
176 
177 mitk::pa::InSilicoTissueVolume::Pointer mitk::pa::IOUtil::LoadInSilicoTissueVolumeFromNrrdFile(std::string nrrdFile)
178 {
179  MITK_INFO << "Initializing ComposedVolume by nrrd...";
180  auto inputImage = mitk::IOUtil::Load<mitk::Image>(nrrdFile);
181 
182  auto tissueParameters = TissueGeneratorParameters::New();
183 
184  unsigned int xDim = inputImage->GetDimensions()[1];
185  unsigned int yDim = inputImage->GetDimensions()[0];
186  unsigned int zDim = inputImage->GetDimensions()[2];
187  tissueParameters->SetXDim(xDim);
188  tissueParameters->SetYDim(yDim);
189  tissueParameters->SetZDim(zDim);
190 
191  double xSpacing = inputImage->GetGeometry(0)->GetSpacing()[1];
192  double ySpacing = inputImage->GetGeometry(0)->GetSpacing()[0];
193  double zSpacing = inputImage->GetGeometry(0)->GetSpacing()[2];
194 
195  if ((xSpacing - ySpacing) > mitk::eps || (xSpacing - zSpacing) > mitk::eps || (ySpacing - zSpacing) > mitk::eps)
196  {
197  throw mitk::Exception("Cannot handle unequal spacing.");
198  }
199 
200  tissueParameters->SetVoxelSpacingInCentimeters(xSpacing);
201 
202  mitk::PropertyList::Pointer propertyList = inputImage->GetPropertyList();
203 
204  mitk::ImageReadAccessor readAccess0(inputImage, inputImage->GetVolumeData(0));
205  auto* m_AbsorptionArray = new double[xDim*yDim*zDim];
206  memcpy(m_AbsorptionArray, readAccess0.GetData(), xDim*yDim*zDim * sizeof(double));
207  auto absorptionVolume = Volume::New(m_AbsorptionArray, xDim, yDim, zDim, xSpacing);
208 
209  mitk::ImageReadAccessor readAccess1(inputImage, inputImage->GetVolumeData(1));
210  auto* m_ScatteringArray = new double[xDim*yDim*zDim];
211  memcpy(m_ScatteringArray, readAccess1.GetData(), xDim*yDim*zDim * sizeof(double));
212  auto scatteringVolume = Volume::New(m_ScatteringArray, xDim, yDim, zDim, xSpacing);
213 
214  mitk::ImageReadAccessor readAccess2(inputImage, inputImage->GetVolumeData(2));
215  auto* m_AnisotropyArray = new double[xDim*yDim*zDim];
216  memcpy(m_AnisotropyArray, readAccess2.GetData(), xDim*yDim*zDim * sizeof(double));
217  auto anisotropyVolume = Volume::New(m_AnisotropyArray, xDim, yDim, zDim, xSpacing);
218 
219  Volume::Pointer segmentationVolume;
220 
221  if (inputImage->GetDimension() == 4)
222  {
223  mitk::ImageReadAccessor readAccess3(inputImage, inputImage->GetVolumeData(3));
224  auto* m_SegmentationArray = new double[xDim*yDim*zDim];
225  memcpy(m_SegmentationArray, readAccess3.GetData(), xDim*yDim*zDim * sizeof(double));
226  segmentationVolume = Volume::New(m_SegmentationArray, xDim, yDim, zDim, xSpacing);
227  }
228 
229  return mitk::pa::InSilicoTissueVolume::New(absorptionVolume, scatteringVolume,
230  anisotropyVolume, segmentationVolume, tissueParameters, propertyList);
231 }
232 
233 mitk::pa::FluenceYOffsetPair::Pointer mitk::pa::IOUtil::LoadFluenceSimulation(std::string fluenceSimulation)
234 {
235  MITK_INFO << "Adding slice...";
236 
237  mitk::Image::Pointer inputImage = mitk::IOUtil::Load<mitk::Image>(fluenceSimulation);
238 
239  auto yOffsetProperty = inputImage->GetProperty("y-offset");
240 
241  if (yOffsetProperty.IsNull())
242  mitkThrow() << "No \"y-offset\" property found in fluence file!";
243 
244  std::string yOff = yOffsetProperty->GetValueAsString();
245  MITK_INFO << "Reading y Offset: " << yOff;
246 #ifdef __linux__
247  std::replace(yOff.begin(), yOff.end(), '.', ',');
248 #endif // __linux__
249  double yOffset = std::stod(yOff);
250  MITK_INFO << "Converted offset " << yOffset;
251  return FluenceYOffsetPair::New(Volume::New(inputImage), yOffset);
252 }
static std::vector< std::string > GetListOfAllNrrdFilesInDirectory(std::string directory, bool keepFileFormat=false)
returns a list of all .nrrd files in a given directory
#define MITK_INFO
Definition: mitkLogMacros.h:18
#define MITK_ERROR
Definition: mitkLogMacros.h:20
static int GetNumberOfNrrdFilesInDirectory(std::string directory)
returns the number of .nrrd files in a given directory
#define MITK_DEBUG
Definition: mitkLogMacros.h:22
float yOffset
Definition: MitkMCxyz.cpp:622
static std::map< Position, Volume::Pointer > LoadFluenceContributionMaps(std::string foldername, double blur, int *progress, bool log10=false)
LoadFluenceContributionMaps loads multiple nrrd files from a given folder and sorts them into a posit...
static InSilicoTissueVolume::Pointer New(mitk::pa::Volume::Pointer absorptionVolume, Volume::Pointer scatteringVolume, Volume::Pointer anisotropyVolume, Volume::Pointer segmentationVolume, TissueGeneratorParameters::Pointer tissueParameters, mitk::PropertyList::Pointer propertyList)
static Volume::Pointer LoadNrrd(std::string filename, double sigma=0)
loads a nrrd file from a given filename.
static FluenceYOffsetPair::Pointer LoadFluenceSimulation(std::string fluenceSimulation)
LoadFluenceSimulation Adds a MC Simulation nrrd file to this composed volume. The given file needs to...
static void Log10Image(Volume::Pointer image)
static bool DoesFileHaveEnding(std::string const &fullString, std::string const &ending)
convenience method to check wether a given strings ends on a given substring.
An object of this class represents an exception of MITK. Please don&#39;t instantiate exceptions manually...
Definition: mitkException.h:45
#define mitkThrow()
static std::vector< std::string > GetAllChildfoldersFromFolder(std::string folderPath)
returns all child folders from a folder if there is no .nrrd file in the folder If there IS a ...
static const char * replace[]
This is a dictionary to replace long names of classes, modules, etc. to shorter versions in the conso...
MITKCORE_EXPORT const ScalarType eps
static void GaussianBlur3D(Volume::Pointer paVolume, double sigma)
applies a Gaussian blur to an image
static InSilicoTissueVolume::Pointer LoadInSilicoTissueVolumeFromNrrdFile(std::string filePath)
loads an in silico tissue volume from a saved nrrd file.
ImageReadAccessor class to get locked read access for a particular image part.
static Volume::Pointer New(double *data, unsigned int xDim, unsigned int yDim, unsigned int zDim, double spacing)
returns smartpointer reference to a new instance of this objects. The given data array will be freed ...
static std::vector< int > splitString(const std::string &s, const char *delim)