Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkVtkSurfaceReader.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 #include "mitkVtkSurfaceReader.h"
13 #include <itksys/SystemTools.hxx>
14 #include <mitkSurface.h>
15 #include <vtkDataReader.h>
16 #include <vtkPolyDataReader.h>
17 #include <vtkXMLPolyDataReader.h>
18 
20 {
21 }
22 
24 {
25 }
26 
28 {
29  if (m_FileName != "")
30  {
31  bool success = false;
32  MITK_INFO << "Loading " << m_FileName << " as vtk" << std::endl;
33 
34  std::string ext = itksys::SystemTools::GetFilenameLastExtension(m_FileName);
35  ext = itksys::SystemTools::LowerCase(ext);
36  if (ext == ".vtk")
37  {
39  vtkDataReader *chooser = vtkDataReader::New();
40  chooser->SetFileName(m_FileName.c_str());
41  if (chooser->IsFilePolyData())
42  {
44  itkDebugMacro(<< "PolyData");
45  vtkPolyDataReader *reader = vtkPolyDataReader::New();
46  reader->SetFileName(m_FileName.c_str());
47  reader->Update();
48 
49  if (reader->GetOutput() != nullptr)
50  {
51  mitk::Surface::Pointer output = this->GetOutput();
52  output->SetVtkPolyData(reader->GetOutput());
53  success = true;
54  }
55  reader->Delete();
56  }
57  chooser->Delete();
58  }
59  else if (ext == ".vtp")
60  {
61  vtkXMLPolyDataReader *reader = vtkXMLPolyDataReader::New();
62  if (reader->CanReadFile(m_FileName.c_str()))
63  {
65  itkDebugMacro(<< "XMLPolyData");
66  reader->SetFileName(m_FileName.c_str());
67  reader->Update();
68 
69  if (reader->GetOutput() != nullptr)
70  {
71  mitk::Surface::Pointer output = this->GetOutput();
72  output->SetVtkPolyData(reader->GetOutput());
73  success = true;
74  }
75  reader->Delete();
76  }
77  }
78  if (!success)
79  {
80  itkWarningMacro(<< " ... sorry, this .vtk format is not supported yet.");
81  }
82  }
83 }
84 
85 bool mitk::VtkSurfaceReader::CanReadFile(const std::string filename,
86  const std::string /*filePrefix*/,
87  const std::string /*filePattern*/)
88 {
89  // First check the extension
90  if (filename == "")
91  return false;
92 
93  std::string ext = itksys::SystemTools::GetFilenameLastExtension(filename);
94  ext = itksys::SystemTools::LowerCase(ext);
95  if (ext == ".vtk")
96  {
97  vtkDataReader *chooser = vtkDataReader::New();
98  chooser->SetFileName(filename.c_str());
99  if (!chooser->IsFilePolyData())
100  {
101  chooser->Delete();
102  return false;
103  }
104  chooser->Delete();
105  }
106  else if (ext == ".vtp")
107  {
108  vtkXMLPolyDataReader *chooser = vtkXMLPolyDataReader::New();
109  if (!chooser->CanReadFile(filename.c_str()))
110  {
111  chooser->Delete();
112  return false;
113  }
114  chooser->Delete();
115  }
116  else
117  return false;
118 
119  return true;
120 }
#define MITK_INFO
Definition: mitkLogMacros.h:18
static bool CanReadFile(const std::string filename, const std::string filePrefix, const std::string filePattern)
OutputType * GetOutput()