Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
mitkFiberBundleVtkWriter.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 <vtkSmartPointer.h>
19 #include <vtkCleanPolyData.h>
20 #include <itksys/SystemTools.hxx>
21 #include <mitkTrackvis.h>
22 #include <itkSize.h>
23 #include <vtkFloatArray.h>
24 #include <vtkCellData.h>
25 #include <vtkPointData.h>
26 #include <mitkAbstractFileWriter.h>
27 #include <mitkCustomMimeType.h>
28 #include <vtkXMLPolyDataWriter.h>
30 
32  : mitk::AbstractFileWriter(mitk::FiberBundle::GetStaticNameOfClass(), mitk::DiffusionIOMimeTypes::FIBERBUNDLE_VTK_MIMETYPE_NAME(), "VTK Fiber Bundle Writer")
33 {
34  Options defaultOptions;
35  defaultOptions["Save as binary file"] = true;
36  defaultOptions["Save as xml file (vtp style)"] = false;
37  defaultOptions["Save color information"] = false;
38  defaultOptions["Save fiber weights"] = true;
39  this->SetDefaultOptions(defaultOptions);
41 }
42 
44  :mitk::AbstractFileWriter(other)
45 {}
46 
48 {}
49 
51 {
52  return new mitk::FiberBundleVtkWriter(*this);
53 }
54 
56 {
57 
58  std::ostream* out;
59  std::ofstream outStream;
60 
61  if( this->GetOutputStream() )
62  {
63  out = this->GetOutputStream();
64  }else{
65  outStream.open( this->GetOutputLocation().c_str() );
66  out = &outStream;
67  }
68 
69  if ( !out->good() )
70  {
71  mitkThrow() << "Stream not good.";
72  }
73 
74  try
75  {
76  const std::string& locale = "C";
77  const std::string& currLocale = setlocale( LC_ALL, NULL );
78  setlocale(LC_ALL, locale.c_str());
79 
80  std::locale previousLocale(out->getloc());
81  std::locale I("C");
82  out->imbue(I);
83 
84  std::string filename = this->GetOutputLocation().c_str();
85 
86  mitk::FiberBundle::ConstPointer input = dynamic_cast<const mitk::FiberBundle*>(this->GetInput());
87  std::string ext = itksys::SystemTools::GetFilenameLastExtension(this->GetOutputLocation().c_str());
88  Options options = this->GetOptions();
89 
90  vtkSmartPointer<vtkPolyData> fibPoly = input->GetFiberPolyData();
91  if (us::any_cast<bool>(options["Save fiber weights"]))
92  {
93  MITK_INFO << "Adding fiber weight information";
94  fibPoly->GetCellData()->AddArray(input->GetFiberWeights());
95  }
96  else if (fibPoly->GetCellData()->HasArray("FIBER_WEIGHTS"))
97  fibPoly->GetCellData()->RemoveArray("FIBER_WEIGHTS");
98  if (us::any_cast<bool>(options["Save color information"]))
99  {
100  MITK_INFO << "Adding color information";
101  fibPoly->GetPointData()->AddArray(input->GetFiberColors());
102  }
103  else if (fibPoly->GetPointData()->HasArray("FIBER_COLORS"))
104  fibPoly->GetPointData()->RemoveArray("FIBER_COLORS");
105 
106  // default extension is .fib
107  if(ext == "")
108  {
109  ext = ".fib";
110  this->SetOutputLocation(this->GetOutputLocation() + ext);
111  }
112 
113  if (us::any_cast<bool>(options["Save as xml file (vtp style)"]))
114  {
115  vtkSmartPointer<vtkXMLPolyDataWriter> writer = vtkSmartPointer<vtkXMLPolyDataWriter>::New();
116  writer->SetInputData(fibPoly);
117  writer->SetFileName(filename.c_str());
118  if (us::any_cast<bool>(options["Save as binary file"]))
119  {
120  MITK_INFO << "Writing fiber bundle as vtk binary file";
121  writer->SetDataModeToBinary();
122  }
123  else
124  {
125  MITK_INFO << "Writing fiber bundle as vtk ascii file";
126  writer->SetDataModeToAscii();
127  }
128  writer->Write();
129  }
130  else
131  {
132  vtkSmartPointer<vtkPolyDataWriter> writer = vtkSmartPointer<vtkPolyDataWriter>::New();
133  writer->SetInputData(fibPoly);
134  writer->SetFileName(filename.c_str());
135  if (us::any_cast<bool>(options["Save as binary file"]))
136  {
137  MITK_INFO << "Writing fiber bundle as vtk binary file";
138  writer->SetFileTypeToBinary();
139  }
140  else
141  {
142  MITK_INFO << "Writing fiber bundle as vtk ascii file";
143  writer->SetFileTypeToASCII();
144  }
145  writer->Write();
146  }
147 
148  setlocale(LC_ALL, currLocale.c_str());
149  MITK_INFO << "Fiber bundle written";
150  }
151  catch(...)
152  {
153  throw;
154  }
155 }
#define MITK_INFO
Definition: mitkLogMacros.h:22
virtual void Write() override
Write the base data to the specified location or output stream.
DataCollection - Class to facilitate loading/accessing structured data.
std::map< std::string, us::Any > Options
Options for reading or writing data.
Definition: mitkIFileIO.h:73
void SetDefaultOptions(const Options &defaultOptions)
static const std::string filename
#define mitkThrow()
Base Class for Fiber Bundles;.
static const char * GetStaticNameOfClass()
virtual FiberBundleVtkWriter * Clone() const override
us::ServiceRegistration< IFileWriter > RegisterService(us::ModuleContext *context=us::GetModuleContext())
Base class for writing mitk::BaseData objects to files or streams.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.