Repository navigation
Expand file tree
/
Copy pathPolyDataCompression.cxx
More file actions
188 lines (183 loc) · 6.65 KB
/
Copy pathPolyDataCompression.cxx
File metadata and controls
188 lines (183 loc) · 6.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include "PolyDataCompressionCLP.h"
#include <string>
#include <iostream>
#include <sstream>
#include <cstring>
#include <vtkSmartPointer.h>
#include <vtkPolyData.h>
#include <vtkPolyDataReader.h>
#include <vtkPolyDataWriter.h>
#include <vtkXMLPolyDataReader.h>
#include <vtkXMLPolyDataWriter.h>
#include <vtkZLibDataCompressor.h>
#include <itksys/SystemTools.hxx>
std::string ChangeEndOfFileName ( std::string fileName, std::string change , std::string extension )
{
if( itksys::SystemTools::GetFilenamePath( fileName ).empty() == true )
{
return itksys::SystemTools::GetFilenameWithoutLastExtension( fileName ) + change + "." + extension ;
}
return itksys::SystemTools::GetFilenamePath( fileName )
+ "/" + itksys::SystemTools::GetFilenameWithoutLastExtension( fileName ) + change +"." + extension ;
}
int ReadFile( const char* fileName , std::string extension , vtkSmartPointer< vtkPolyData > &polyData )
{
if( extension == ".vtk" )
{
vtkSmartPointer< vtkPolyDataReader > reader = vtkSmartPointer< vtkPolyDataReader >::New() ;
reader->SetFileName( fileName ) ;
reader->Update() ;
polyData = reader->GetOutput() ;
return reader->GetErrorCode() ;
}
else if( extension == ".vtp" )
{
vtkSmartPointer< vtkXMLPolyDataReader > reader = vtkSmartPointer< vtkXMLPolyDataReader >::New() ;
reader->SetFileName( fileName ) ;
reader->Update() ;
polyData = reader->GetOutput() ;
return reader->GetErrorCode() ;
}
else
{
return 1 ;
}
}
int WriteFile( std::string encoding , std::string extension , const char* outputFileName ,
int compressionLevel , vtkSmartPointer< vtkPolyData > readerPolyData )
{
if( extension == "vtp" )
{
vtkSmartPointer< vtkXMLPolyDataWriter > writer = vtkSmartPointer< vtkXMLPolyDataWriter >::New() ;
writer->SetFileName( outputFileName ) ;
writer->SetInputData( readerPolyData ) ;
vtkZLibDataCompressor *compressor = dynamic_cast< vtkZLibDataCompressor* > ( writer->GetCompressor() ) ;
if( compressor )
{
compressor->SetCompressionLevel( compressionLevel ) ;
}
if( encoding == "binary" )
{
writer->SetDataModeToBinary() ;
}
else if( encoding == "appended" )
{
writer->SetDataModeToAppended() ;
}
else if( encoding == "ascii" )
{
writer->SetDataModeToAscii() ;
}
else // should not arrive here. tested in main before already and program should exit at that time.
{
return -1 ;
}
writer->Update() ;
return writer->GetErrorCode() ;
}
else
{
vtkSmartPointer< vtkPolyDataWriter > writer = vtkSmartPointer< vtkPolyDataWriter >::New() ;
writer->SetFileName( outputFileName ) ;
writer->SetInputData( readerPolyData ) ;
if( encoding =="binary" )
{
writer->SetFileTypeToBinary();
}
else if( encoding == "ascii" )
{
writer->SetFileTypeToASCII() ;
}
else
{
return -1 ;
}
writer->Update() ;
return writer->GetErrorCode() ;
}
}
int main( int argc, char *argv[] )
{
std::vector< std::string > fileNameList ;
bool flagFound = false ;
for( int i = 0 ; i < argc ; i++ )
{
if( flagFound )
{
if( argv[ i ][ 0 ] == '-' )
{
std::cout << "Please write all your flags before the -f flag. -f flag should be the last flag. "<< std::endl ;
return EXIT_FAILURE ;
}
else
{
fileNameList.push_back( argv[ i ] ) ;
}
}
if( !strcmp( argv[ i ] , "-f" ) || !strcmp( argv[ i ] , "--fileNameList" ) )
{
flagFound = true ;
}
}
argc = argc - ( fileNameList.empty() ? 0 : fileNameList.size() - 1 ) ;
PARSE_ARGS ;
if( !flagFound )
{
std::cerr << "Missing fileNameList flag, please give at least one file." << std::endl ;
return EXIT_FAILURE ;
}
std::string append = "-compressed" ;
for( size_t i = 0 ; i < fileNameList.size() ; i++ )
{
std::string testFileName = itksys::SystemTools::GetFilenameName( ( fileNameList[ i ] ) ) ;
if( testFileName.find( append + ".vtp" ) != std::string::npos ) //looks if "-compressed" is already at the end of the filename -> if yes, the file will be skipped
{
std::cout << fileNameList[ i ] << " already compressed." << std::endl ;
continue ;
}
else
{
vtkSmartPointer< vtkPolyData > readerPolyData = vtkSmartPointer< vtkPolyData >::New() ;
std::ifstream infile( fileNameList[ i ].c_str() ) ;
if( !infile )
{
std::cerr << "Unable to find input file: " << fileNameList[ i ] << std::endl ;
return EXIT_FAILURE ;
}
if( ReadFile( fileNameList[ i ].c_str() , itksys::SystemTools::GetFilenameLastExtension( fileNameList[ i ] ) , readerPolyData ) != 0 )
{
std::cerr << "Unable to open input file: " << fileNameList[ i ] << std::endl ;
return EXIT_FAILURE ;
}
if( !readerPolyData.GetPointer() )
{
return EXIT_FAILURE ;
}
std::cout << "Compressing: " << fileNameList[ i ] << std::endl ;
if( overwrite == 0 )
{
std::string outputFileName = ChangeEndOfFileName( fileNameList[ i ] , append , extension ) ;
if( WriteFile( encoding , extension , outputFileName.c_str() , compressionLevel , readerPolyData ) )
{
std::cerr << "Unable to write output file." << std::endl ;
return EXIT_FAILURE ;
}
}
else
{
std::string outputFileName = ChangeEndOfFileName( fileNameList[ i ] , append , extension ) ;
if( WriteFile( encoding , extension , outputFileName.c_str() , compressionLevel , readerPolyData ) )
{
std::cerr << "Unable to write temporary compressed file." << std::endl ;
return EXIT_FAILURE ;
}
if( WriteFile( encoding , extension , fileNameList[ i ].c_str() , compressionLevel , readerPolyData ) )
{
std::cerr << "Unable to write output file." << std::endl ;
return EXIT_FAILURE ;
}
}
}
}
return EXIT_SUCCESS ;
}