-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy path.#itkLabeledPointSetFileWriter.txx.1.1.1.1
executable file
·316 lines (269 loc) · 7.77 KB
/
.#itkLabeledPointSetFileWriter.txx.1.1.1.1
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: itkLabeledPointSetFileWriter.txx,v $
Language: C++
Date: $Date: 2008/10/18 00:21:04 $
Version: $Revision: 1.1.1.1 $
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#ifndef __itkLabeledPointSetFileWriter_txx
#define __itkLabeledPointSetFileWriter_txx
#include "itkLabeledPointSetFileWriter.h"
#include "itkImageFileWriter.h"
#include <fstream>
namespace itk
{
//
// Constructor
//
template<class TInputMesh>
LabeledPointSetFileWriter<TInputMesh>
::LabeledPointSetFileWriter()
{
this->m_Input = NULL;
this->m_FileName = "";
this->m_ImageSize.Fill( 0 );
}
//
// Destructor
//
template<class TInputMesh>
LabeledPointSetFileWriter<TInputMesh>
::~LabeledPointSetFileWriter()
{
}
//
// Set the input mesh
//
template<class TInputMesh>
void
LabeledPointSetFileWriter<TInputMesh>
::SetInput(InputMeshType * input)
{
this->m_Input = input;
}
//
// Write the input mesh to the output file
//
template<class TInputMesh>
void LabeledPointSetFileWriter<TInputMesh>
::Update()
{
this->GenerateData();
}
//
// Write the input mesh to the output file
//
template<class TInputMesh>
void LabeledPointSetFileWriter<TInputMesh>
::Write()
{
this->GenerateData();
}
template<class TInputMesh>
void
LabeledPointSetFileWriter<TInputMesh>
::GenerateData()
{
if( this->m_FileName == "" )
{
itkExceptionMacro( "No FileName" );
return;
}
if( this->m_ImageSize[0] == 0 )
{
this->m_ImageSize.Fill( 100 );
this->m_ImageOrigin.CastFrom(
this->m_Input->GetBoundingBox()->GetMinimum() );
for( unsigned int d = 0; d < Dimension; d++ )
{
this->m_ImageSpacing[d] = (
this->m_Input->GetBoundingBox()->GetMaximum()[d] -
this->m_Input->GetBoundingBox()->GetMinimum()[d] )
/ static_cast<double>( this->m_ImageSize[d] + 1 );
}
this->m_ImageDirection.SetIdentity();
}
//
// Read output file
//
std::ofstream outputFile( m_FileName.c_str() );
if( !outputFile.is_open() )
{
itkExceptionMacro( "Unable to open file\n"
"outputFilename= " << m_FileName );
return;
}
else
{
outputFile.close();
}
/**
* Get filename extension
*/
std::string::size_type pos = this->m_FileName.rfind( "." );
std::string extension( this->m_FileName, pos+1, this->m_FileName.length()-1 );
if( extension == "txt" )
{
this->WriteLandmarksToAvantsFile();
}
else if( extension == "vtk" )
{
this->WriteLandmarksToVTKFile();
}
else
{
try
{
this->WriteLandmarksToImageFile();
}
catch(...)
{
itkExceptionMacro( "Unknown extension: " << extension );
}
}
}
template<class TInputMesh>
void
LabeledPointSetFileWriter<TInputMesh>
::WriteLandmarksToVTKFile()
{
//
// Write to output file
//
std::ofstream outputFile( this->m_FileName.c_str() );
outputFile << "# vtk DataFile Version 2.0" << std::endl;
outputFile << "File written by itkLabeledPointSetFileWriter" << std::endl;
outputFile << "ASCII" << std::endl;
outputFile << "DATASET POLYDATA" << std::endl;
// POINTS go first
unsigned int numberOfPoints = this->m_Input->GetNumberOfPoints();
outputFile << "POINTS " << numberOfPoints << " float" << std::endl;
typename InputMeshType::PointsContainerIterator pointIterator
= this->m_Input->GetPoints()->Begin();
typename InputMeshType::PointsContainerIterator pointEnd
= this->m_Input->GetPoints()->End();
while( pointIterator != pointEnd )
{
PointType point = pointIterator.Value();
outputFile << point[0] << " " << point[1];
if( Dimension == 2 )
{
outputFile << " 0 " << std::endl;
}
else if( Dimension == 3 )
{
outputFile << " " << point[2] << " " << std::endl;
}
pointIterator++;
}
outputFile << std::endl;
outputFile << "POINT_DATA " << numberOfPoints << std::endl;
outputFile << "SCALARS pointLabels long 1" << std::endl;
outputFile << "LOOKUP_TABLE default" << std::endl;
typename InputMeshType::PointDataContainerIterator pointDataIterator
= this->m_Input->GetPointData()->Begin();
typename InputMeshType::PointDataContainerIterator pointDataEnd
= this->m_Input->GetPointData()->End();
while( pointDataIterator != pointDataEnd )
{
outputFile << pointDataIterator.Value() << " ";
pointDataIterator++;
}
outputFile << std::endl;
outputFile.close();
}
template<class TInputMesh>
void
LabeledPointSetFileWriter<TInputMesh>
::WriteLandmarksToAvantsFile()
{
//
// Write to output file
//
std::ofstream outputFile( this->m_FileName.c_str() );
outputFile << "0 0 0 0" << std::endl;
if( this->m_Input->GetNumberOfPoints() > 0 )
{
typename InputMeshType::PointsContainerIterator pointIterator
= this->m_Input->GetPoints()->Begin();
typename InputMeshType::PointsContainerIterator pointEnd
= this->m_Input->GetPoints()->End();
typename InputMeshType::PointDataContainerIterator pointDataIterator
= this->m_Input->GetPointData()->Begin();
while( pointIterator != pointEnd )
{
PointType point = pointIterator.Value();
outputFile << point[0] << " " << point[1];
if( Dimension == 2 )
{
outputFile << " 0 ";
}
else if( Dimension == 3 )
{
outputFile << " " << point[2] << " ";
}
outputFile << pointDataIterator.Value() << std::endl;
pointIterator++;
pointDataIterator++;
}
}
outputFile << "0 0 0 0" << std::endl;
outputFile.close();
}
template<class TInputMesh>
void
LabeledPointSetFileWriter<TInputMesh>
::WriteLandmarksToImageFile()
{
typename LabeledPointSetImageType::Pointer outputImage
= LabeledPointSetImageType::New();
outputImage->SetDirection( this->m_ImageDirection );
outputImage->SetRegions( this->m_ImageSize );
outputImage->SetOrigin( this->m_ImageOrigin );
outputImage->SetSpacing( this->m_ImageSpacing );
outputImage->Allocate();
outputImage->FillBuffer( NumericTraits<PixelType>::Zero );
if( this->m_Input->GetNumberOfPoints() > 0 )
{
typename InputMeshType::PointsContainerIterator pointIterator
= this->m_Input->GetPoints()->Begin();
typename InputMeshType::PointsContainerIterator pointEnd
= this->m_Input->GetPoints()->End();
typename InputMeshType::PointDataContainerIterator pointDataIterator
= this->m_Input->GetPointData()->Begin();
while( pointIterator != pointEnd )
{
PointType point = pointIterator.Value();
PixelType label = pointDataIterator.Value();
typename LabeledPointSetImageType::IndexType index;
typename LabeledPointSetImageType::PointType ipoint;
ipoint.CastFrom( point );
if( outputImage->TransformPhysicalPointToIndex( ipoint, index ) )
{
outputImage->SetPixel( index, label );
}
pointIterator++;
pointDataIterator++;
}
}
typedef ImageFileWriter<LabeledPointSetImageType> WriterType;
typename WriterType::Pointer writer = WriterType::New();
writer->SetFileName( this->m_FileName.c_str() );
writer->SetInput( outputImage );
writer->Update();
}
template<class TInputMesh>
void
LabeledPointSetFileWriter<TInputMesh>
::PrintSelf( std::ostream& os, Indent indent ) const
{
Superclass::PrintSelf(os,indent);
os << indent << "FileName: " << this->m_FileName << std::endl;
}
} //end of namespace itk
#endif