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> 37 bool File::OpenModel(
const string filename, vtkSmartPointer<vtkPolyData> &data)
39 bool legacy =
false, wavefront =
false, stanford =
false, stereoLith =
false;
42 if (extension ==
"vtk")
44 else if (extension ==
"obj")
46 else if (extension ==
"ply")
48 else if(extension ==
"stl")
52 std::cerr <<
"PolyData pointer is not NULL. May get a memory leak." << endl;
54 vtkSmartPointer<vtkErrorWarning> errorObserver = vtkSmartPointer<vtkErrorWarning>::New();
57 vtkSmartPointer<vtkPolyDataReader> reader = vtkSmartPointer<vtkPolyDataReader>::New();
58 reader->SetFileName(filename.c_str());
59 reader->AddObserver(vtkCommand::ErrorEvent, errorObserver);
62 if (!errorObserver->ReportsFailure())
64 data = reader->GetOutput();
70 vtkSmartPointer<vtkOBJReader> reader = vtkSmartPointer<vtkOBJReader>::New();
71 reader->SetFileName(filename.c_str());
72 reader->AddObserver(vtkCommand::ErrorEvent, errorObserver);
75 if (!errorObserver->ReportsFailure())
77 data = reader->GetOutput();
83 vtkSmartPointer<vtkPLYReader> reader = vtkSmartPointer<vtkPLYReader>::New();
84 reader->SetFileName(filename.c_str());
85 reader->AddObserver(vtkCommand::ErrorEvent, errorObserver);
88 if (!errorObserver->ReportsFailure())
90 data = reader->GetOutput();
96 vtkSmartPointer<vtkSTLReader> reader = vtkSmartPointer<vtkSTLReader>::New();
97 reader->SetFileName(filename.c_str());
98 reader->AddObserver(vtkCommand::ErrorEvent, errorObserver);
101 if (!errorObserver->ReportsFailure())
103 data = reader->GetOutput();
109 vtkSmartPointer<vtkXMLPolyDataReader> reader = vtkSmartPointer<vtkXMLPolyDataReader>::New();
110 reader->SetFileName(filename.c_str());
111 reader->AddObserver(vtkCommand::ErrorEvent, errorObserver);
114 if (!errorObserver->ReportsFailure())
116 data = reader->GetOutput();
121 cerr <<
"Reader Encountered the following error." << endl;
122 cerr << errorObserver->GetMessage() << endl;
128 collection = vtkSmartPointer<vtkPolyDataCollection>::New();
130 for(std::vector<string>::iterator name = filenames.begin(); name != filenames.end(); name ++)
132 vtkSmartPointer<vtkPolyData> surface;
137 collection->AddItem( surface );
143 bool File::SaveModel(
const string filename, vtkSmartPointer<vtkPolyData> data,
const bool binary)
145 bool legacy =
false, stanford =
false, stereoLith =
false;
148 if (extension ==
"vtk")
150 else if (extension ==
"ply")
152 else if(extension ==
"stl")
157 std::cerr <<
"PolyData pointer is NULL. Not saving." << endl;
163 vtkSmartPointer<vtkPolyDataWriter> writer = vtkSmartPointer<vtkPolyDataWriter>::New();
164 writer->SetFileName(filename.c_str());
165 writer->SetInput(data);
170 vtkSmartPointer<vtkPLYWriter> writer = vtkSmartPointer<vtkPLYWriter>::New();
171 writer->SetFileName(filename.c_str());
172 writer->SetInput(data);
177 vtkSmartPointer<vtkSTLWriter> writer = vtkSmartPointer<vtkSTLWriter>::New();
178 writer->SetFileName(filename.c_str());
179 writer->SetInput(data);
184 vtkSmartPointer<vtkXMLPolyDataWriter> writer = vtkSmartPointer<vtkXMLPolyDataWriter>::New();
185 writer->SetFileName(filename.c_str());
186 writer->SetInput(data);
188 writer->SetDataModeToBinary();
197 collection->InitTraversal();
198 for(std::vector<string>::iterator name = filenames.begin(); name != filenames.end(); name ++)
209 vtkErrorWarning::vtkErrorWarning()
214 vtkErrorWarning::~vtkErrorWarning()
219 void vtkErrorWarning::Execute(vtkObject *caller,
unsigned long observedType,
void* message)
221 m_Message = (
const char*) message;
223 if(observedType == vtkCommand::ErrorEvent)
224 m_ErrorEncountered =
true;
225 if(observedType == vtkCommand::WarningEvent)
226 m_WarningEncountered =
true;
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 *...
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 *...
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 *...
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 *...
static std::string GetFileExtension(const std::string &filename)
Returns the file extension (in lower case) of the given filename.