SMILX  1.01
milxFile_Threaded.cxx
1 /*=========================================================================
2  Program: milxSMILI
3  Module: milxModel
4  Author: Shekhar Chandra
5  Language: C++
6  Created: 18 Apr 2011 12:03:00 EST
7 
8  Copyright: (c) CSIRO, Australia
9 
10  This software is protected by international copyright laws.
11  Any unauthorised copying, distribution or reverse engineering is prohibited.
12 
13  Licence:
14  All rights in this Software are reserved to CSIRO. You are only permitted
15  to have this Software in your possession and to make use of it if you have
16  agreed to a Software License with CSIRO.
17 
18  BioMedIA Lab: http://www.ict.csiro.au/BioMedIA/
19 =========================================================================*/
20 #include "milxFile.h"
21 //VTK
22 #include <vtkOBJReader.h>
23 #include <vtkSTLReader.h>
24 #include <vtkSTLWriter.h>
25 #include <vtkPLYReader.h>
26 #include <vtkPLYWriter.h>
27 #include <vtkPolyDataReader.h>
28 #include <vtkPolyDataWriter.h>
29 #include <vtkXMLPolyDataReader.h>
30 #include <vtkXMLPolyDataWriter.h>
31 #include <vtkXMLImageDataReader.h>
32 #include <vtkXMLImageDataWriter.h>
33 
34 namespace milx
35 {
36 
37 bool File::OpenModel(const string filename, vtkSmartPointer<vtkPolyData> &data)
38 {
39  bool legacy = false, wavefront = false, stanford = false, stereoLith = false;
40  string extension = GetFileExtension(filename);
41 
42  if (extension == "vtk")
43  legacy = true; //Load legacy VTK file
44  else if (extension == "obj")
45  wavefront = true;
46  else if (extension == "ply")
47  stanford = true;
48  else if(extension == "stl")
49  stereoLith = true;
50 
51  if(data != NULL)
52  std::cerr << "PolyData pointer is not NULL. May get a memory leak." << endl;
53 
54  vtkSmartPointer<vtkErrorWarning> errorObserver = vtkSmartPointer<vtkErrorWarning>::New();
55  if (legacy)
56  {
57  vtkSmartPointer<vtkPolyDataReader> reader = vtkSmartPointer<vtkPolyDataReader>::New();
58  reader->SetFileName(filename.c_str());
59  reader->AddObserver(vtkCommand::ErrorEvent, errorObserver);
60  reader->Update();
61 
62  if (!errorObserver->ReportsFailure())
63  {
64  data = reader->GetOutput();
65  return true;
66  }
67  }
68  else if (wavefront)
69  {
70  vtkSmartPointer<vtkOBJReader> reader = vtkSmartPointer<vtkOBJReader>::New();
71  reader->SetFileName(filename.c_str());
72  reader->AddObserver(vtkCommand::ErrorEvent, errorObserver);
73  reader->Update();
74 
75  if (!errorObserver->ReportsFailure())
76  {
77  data = reader->GetOutput();
78  return true;
79  }
80  }
81  else if (stanford)
82  {
83  vtkSmartPointer<vtkPLYReader> reader = vtkSmartPointer<vtkPLYReader>::New();
84  reader->SetFileName(filename.c_str());
85  reader->AddObserver(vtkCommand::ErrorEvent, errorObserver);
86  reader->Update();
87 
88  if (!errorObserver->ReportsFailure())
89  {
90  data = reader->GetOutput();
91  return true;
92  }
93  }
94  else if(stereoLith)
95  {
96  vtkSmartPointer<vtkSTLReader> reader = vtkSmartPointer<vtkSTLReader>::New();
97  reader->SetFileName(filename.c_str());
98  reader->AddObserver(vtkCommand::ErrorEvent, errorObserver);
99  reader->Update();
100 
101  if (!errorObserver->ReportsFailure())
102  {
103  data = reader->GetOutput();
104  return true;
105  }
106  }
107  else
108  {
109  vtkSmartPointer<vtkXMLPolyDataReader> reader = vtkSmartPointer<vtkXMLPolyDataReader>::New();
110  reader->SetFileName(filename.c_str());
111  reader->AddObserver(vtkCommand::ErrorEvent, errorObserver);
112  reader->Update();
113 
114  if (!errorObserver->ReportsFailure())
115  {
116  data = reader->GetOutput();
117  return true;
118  }
119  }
120 
121  cerr << "Reader Encountered the following error." << endl;
122  cerr << errorObserver->GetMessage() << endl;
123  return false;
124 }
125 
126 bool File::OpenModelCollection(std::vector<string> filenames, vtkSmartPointer<vtkPolyDataCollection> &collection)
127 {
128  collection = vtkSmartPointer<vtkPolyDataCollection>::New();
129 
130  for(std::vector<string>::iterator name = filenames.begin(); name != filenames.end(); name ++)
131  {
132  vtkSmartPointer<vtkPolyData> surface;
133 
134  if( !File::OpenModel(*name, surface) ) //Error printed inside
135  return false;
136 
137  collection->AddItem( surface );
138  }
139 
140  return true;
141 }
142 
143 bool File::SaveModel(const string filename, vtkSmartPointer<vtkPolyData> data, const bool binary)
144 {
145  bool legacy = false, stanford = false, stereoLith = false;
146  string extension = GetFileExtension(filename);
147 
148  if (extension == "vtk")
149  legacy = true; //Save legacy VTK file
150  else if (extension == "ply")
151  stanford = true; //Save Stanford Poly file
152  else if(extension == "stl")
153  stereoLith = true; //STL files
154 
155  if(data == NULL)
156  {
157  std::cerr << "PolyData pointer is NULL. Not saving." << endl;
158  return false;
159  }
160 
161  if (legacy)
162  {
163  vtkSmartPointer<vtkPolyDataWriter> writer = vtkSmartPointer<vtkPolyDataWriter>::New();
164  writer->SetFileName(filename.c_str());
165  writer->SetInput(data);
166  writer->Write();
167  }
168  else if (stanford)
169  {
170  vtkSmartPointer<vtkPLYWriter> writer = vtkSmartPointer<vtkPLYWriter>::New();
171  writer->SetFileName(filename.c_str());
172  writer->SetInput(data);
173  writer->Write();
174  }
175  else if(stereoLith)
176  {
177  vtkSmartPointer<vtkSTLWriter> writer = vtkSmartPointer<vtkSTLWriter>::New();
178  writer->SetFileName(filename.c_str());
179  writer->SetInput(data);
180  writer->Write();
181  }
182  else
183  {
184  vtkSmartPointer<vtkXMLPolyDataWriter> writer = vtkSmartPointer<vtkXMLPolyDataWriter>::New();
185  writer->SetFileName(filename.c_str());
186  writer->SetInput(data);
187  if (binary)
188  writer->SetDataModeToBinary();
189  writer->Write();
190  }
191 
192  return true;
193 }
194 
195 bool File::SaveModelCollection(std::vector<string> filenames, vtkSmartPointer<vtkPolyDataCollection> collection)
196 {
197  collection->InitTraversal();
198  for(std::vector<string>::iterator name = filenames.begin(); name != filenames.end(); name ++)
199  {
200  if( !File::SaveModel(*name, collection->GetNextItem()) ) //Error printed inside
201  return false;
202  }
203 
204  return true;
205 }
206 
207 } //end milx namespace
208 
209 vtkErrorWarning::vtkErrorWarning()
210 {
211  Initialize();
212 }
213 
214 vtkErrorWarning::~vtkErrorWarning()
215 {
216 
217 }
218 
219 void vtkErrorWarning::Execute(vtkObject *caller, unsigned long observedType, void* message)
220 {
221  m_Message = (const char*) message;
222 
223  if(observedType == vtkCommand::ErrorEvent)
224  m_ErrorEncountered = true;
225  if(observedType == vtkCommand::WarningEvent)
226  m_WarningEncountered = true;
227 }
static bool OpenModel(const std::string filename, vtkSmartPointer< vtkPolyData > &data)
Opens a model file, which can be a VTK XML, Legacy VTK PolyData File (i.e. either a *...
Definition: milxFile.cxx:488
static bool OpenModelCollection(std::vector< std::string > filenames, vtkSmartPointer< vtkPolyDataCollection > &collection)
Opens model files, which can be a VTK XML, Legacy VTK PolyData File (i.e. either a *...
Definition: milxFile.cxx:611
static bool SaveModelCollection(std::vector< std::string > filenames, vtkSmartPointer< vtkPolyDataCollection > collection, const bool binary=false)
Saves model files, which can be a VTK XML, Legacy VTK PolyData File (i.e. either a *...
Definition: milxFile.cxx:697
static bool SaveModel(const std::string filename, vtkSmartPointer< vtkPolyData > data, const bool binary=false)
Saves a model as a file, which can be a VTK XML or Legacy VTK PolyData File (i.e. either a *...
Definition: milxFile.cxx:628
static std::string GetFileExtension(const std::string &filename)
Returns the file extension (in lower case) of the given filename.
Definition: milxFile.h:472