-
-
Notifications
You must be signed in to change notification settings - Fork 663
/
itkMeshFileWriter.hxx
497 lines (428 loc) · 14.9 KB
/
itkMeshFileWriter.hxx
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/*=========================================================================
*
* Copyright NumFOCUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#ifndef itkMeshFileWriter_hxx
#define itkMeshFileWriter_hxx
#include "itkCommand.h"
#include "itkDataObject.h"
#include "itkMeshConvertPixelTraits.h"
#include "itkMeshIOFactory.h"
#include "itkObjectFactoryBase.h"
#include "vnl/vnl_vector.h"
namespace itk
{
template <typename TInputMesh>
MeshFileWriter<TInputMesh>::MeshFileWriter()
{
m_MeshIO = nullptr;
m_UseCompression = false;
m_FactorySpecifiedMeshIO = false;
m_UserSpecifiedMeshIO = false;
m_FileTypeIsBINARY = false;
}
template <typename TInputMesh>
void
MeshFileWriter<TInputMesh>::SetInput(const InputMeshType * input)
{
this->ProcessObject::SetNthInput(0, const_cast<TInputMesh *>(input));
}
template <typename TInputMesh>
auto
MeshFileWriter<TInputMesh>::GetInput() -> const InputMeshType *
{
if (this->GetNumberOfInputs() < 1)
{
return nullptr;
}
return static_cast<TInputMesh *>(this->ProcessObject::GetInput(0));
}
template <typename TInputMesh>
auto
MeshFileWriter<TInputMesh>::GetInput(unsigned int idx) -> const InputMeshType *
{
return static_cast<TInputMesh *>(this->ProcessObject::GetInput(idx));
}
template <typename TInputMesh>
void
MeshFileWriter<TInputMesh>::Write()
{
const InputMeshType * input = this->GetInput();
itkDebugMacro(<< "Writing an mesh file");
// Make sure input is available
if (input == nullptr)
{
itkExceptionMacro(<< "No input to writer!");
}
// Make sure that we can write the file given the name
if (m_FileName.empty())
{
throw MeshFileWriterException(__FILE__, __LINE__, "FileName must be specified", ITK_LOCATION);
}
if (!(m_UserSpecifiedMeshIO && !m_MeshIO.IsNull()))
{
// Try creating via factory
if (m_MeshIO.IsNull())
{
itkDebugMacro(<< "Attempting factory creation of MeshIO for file: " << m_FileName);
m_MeshIO = MeshIOFactory::CreateMeshIO(m_FileName.c_str(), MeshIOFactory::IOFileModeEnum::WriteMode);
m_FactorySpecifiedMeshIO = true;
}
else
{
if (m_FactorySpecifiedMeshIO && !m_MeshIO->CanWriteFile(m_FileName.c_str()))
{
itkDebugMacro(<< "MeshIO exists but doesn't know how to write file:" << m_FileName);
itkDebugMacro(<< "Attempting creation of MeshIO with a factory for file:" << m_FileName);
m_MeshIO = MeshIOFactory::CreateMeshIO(m_FileName.c_str(), MeshIOFactory::IOFileModeEnum::WriteMode);
m_FactorySpecifiedMeshIO = true;
}
}
}
if (m_MeshIO.IsNull())
{
MeshFileWriterException e(__FILE__, __LINE__);
std::ostringstream msg;
msg << " Could not create IO object for file " << m_FileName.c_str() << std::endl;
msg << " Tried to create one of the following:" << std::endl;
{
for (auto & allobject : ObjectFactoryBase::CreateAllInstance("itkMeshIOBase"))
{
auto * io = dynamic_cast<MeshIOBase *>(allobject.GetPointer());
msg << " " << io->GetNameOfClass() << std::endl;
}
}
msg << " You probably failed to set a file suffix, or" << std::endl;
msg << " set the suffix to an unsupported type." << std::endl;
e.SetDescription(msg.str().c_str());
e.SetLocation(ITK_LOCATION);
throw e;
}
// NOTE: this const_cast<> is due to the lack of const-correctness
// of the ProcessObject.
auto * nonConstInput = const_cast<InputMeshType *>(input);
// Update the input.
// Streaming is not supported at this time.
nonConstInput->SetRequestedRegionToLargestPossibleRegion();
nonConstInput->Update();
if (m_FileTypeIsBINARY)
{
m_MeshIO->SetFileType(IOFileEnum::BINARY);
}
else
{
m_MeshIO->SetFileType(IOFileEnum::ASCII);
}
if (m_UseCompression)
{
m_MeshIO->UseCompressionOn();
}
else
{
m_MeshIO->UseCompressionOff();
}
// Setup the MeshIO
m_MeshIO->SetFileName(m_FileName.c_str());
// Whether write points
if (input->GetPoints() && input->GetNumberOfPoints())
{
m_MeshIO->SetUpdatePoints(true);
m_MeshIO->SetNumberOfPoints(input->GetNumberOfPoints());
m_MeshIO->SetPointDimension(TInputMesh::PointDimension);
m_MeshIO->SetPointComponentType(MeshIOBase::MapComponentType<typename TInputMesh::PointType::ValueType>::CType);
}
// Whether write cells
if (input->GetCells() && input->GetNumberOfCells())
{
SizeValueType cellsBufferSize = 2 * input->GetNumberOfCells();
for (typename TInputMesh::CellsContainerConstIterator ct = input->GetCells()->Begin();
ct != input->GetCells()->End();
++ct)
{
cellsBufferSize += ct->Value()->GetNumberOfPoints();
}
m_MeshIO->SetCellBufferSize(cellsBufferSize);
m_MeshIO->SetUpdateCells(true);
m_MeshIO->SetNumberOfCells(input->GetNumberOfCells());
m_MeshIO->SetCellComponentType(MeshIOBase::MapComponentType<typename TInputMesh::PointIdentifier>::CType);
}
// Whether write point data
if (input->GetPointData() && input->GetPointData()->Size())
{
m_MeshIO->SetUpdatePointData(true);
m_MeshIO->SetNumberOfPointPixels(input->GetPointData()->Size());
// m_MeshIO->SetNumberOfPointPixelComponents(MeshConvertPixelTraits<typename
// TInputMesh::PixelType>::GetNumberOfComponents());
m_MeshIO->SetPixelType(input->GetPointData()->Begin().Value(), true);
}
// Whether write cell data
if (input->GetCellData() && input->GetCellData()->Size())
{
m_MeshIO->SetUpdateCellData(true);
m_MeshIO->SetNumberOfCellPixels(input->GetCellData()->Size());
// m_MeshIO->SetNumberOfCellPixelComponents(MeshConvertPixelTraits<typename
// TInputMesh::CellPixelType>::GetNumberOfComponents());
m_MeshIO->SetPixelType(input->GetCellData()->Begin().Value(), false);
}
this->InvokeEvent(StartEvent());
// Write mesh information
m_MeshIO->WriteMeshInformation();
// write points
if (input->GetPoints() && input->GetNumberOfPoints())
{
WritePoints();
}
// Write cells
if (input->GetCells() && input->GetNumberOfCells())
{
WriteCells();
}
// Write point data
if (input->GetPointData() && input->GetPointData()->Size())
{
WritePointData();
}
// Write cell data
if (input->GetCellData() && input->GetCellData()->Size())
{
WriteCellData();
}
// Write to disk
m_MeshIO->Write();
// Notify end event observers
this->InvokeEvent(EndEvent());
// Release upstream data if requested
this->ReleaseInputs();
}
template <typename TInputMesh>
void
MeshFileWriter<TInputMesh>::WritePoints()
{
const InputMeshType * input = this->GetInput();
itkDebugMacro(<< "Writing points: " << m_FileName);
SizeValueType pointsBufferSize = input->GetNumberOfPoints() * TInputMesh::PointDimension;
auto * buffer = new typename TInputMesh::PointType::ValueType[pointsBufferSize];
CopyPointsToBuffer(buffer);
m_MeshIO->WritePoints(buffer);
delete[] buffer;
}
template <typename TInputMesh>
void
MeshFileWriter<TInputMesh>::WriteCells()
{
itkDebugMacro(<< "Writing cells: " << m_FileName);
SizeValueType cellsBufferSize = m_MeshIO->GetCellBufferSize();
auto * buffer = new typename TInputMesh::PointIdentifier[cellsBufferSize];
CopyCellsToBuffer(buffer);
m_MeshIO->WriteCells(buffer);
delete[] buffer;
}
template <typename TInputMesh>
void
MeshFileWriter<TInputMesh>::WritePointData()
{
const InputMeshType * input = this->GetInput();
itkDebugMacro(<< "Writing point data: " << m_FileName);
if (input->GetPointData()->Size())
{
const SizeValueType numberOfComponents =
input->GetPointData()->Size() * MeshConvertPixelTraits<typename TInputMesh::PixelType>::GetNumberOfComponents(
input->GetPointData()->Begin().Value());
using ValueType = typename itk::NumericTraits<typename TInputMesh::PixelType>::ValueType;
auto * buffer = new ValueType[numberOfComponents];
CopyPointDataToBuffer(buffer);
m_MeshIO->WritePointData(buffer);
delete[] buffer;
}
}
template <typename TInputMesh>
void
MeshFileWriter<TInputMesh>::WriteCellData()
{
const InputMeshType * input = this->GetInput();
itkDebugMacro(<< "Writing cell data: " << m_FileName);
if (input->GetCellData()->Size())
{
const SizeValueType numberOfComponents =
input->GetCellData()->Size() * MeshConvertPixelTraits<typename TInputMesh::CellPixelType>::GetNumberOfComponents(
input->GetCellData()->Begin().Value());
using ValueType = typename itk::NumericTraits<typename TInputMesh::CellPixelType>::ValueType;
auto * buffer = new ValueType[numberOfComponents];
CopyCellDataToBuffer(buffer);
m_MeshIO->WriteCellData(buffer);
delete[] buffer;
}
}
template <typename TInputMesh>
template <typename Output>
void
MeshFileWriter<TInputMesh>::CopyPointsToBuffer(Output * data)
{
const typename InputMeshType::PointsContainer * points = this->GetInput()->GetPoints();
SizeValueType index = NumericTraits<SizeValueType>::ZeroValue();
typename TInputMesh::PointsContainerConstIterator pter = points->Begin();
while (pter != points->End())
{
for (unsigned int jj = 0; jj < TInputMesh::PointDimension; ++jj)
{
data[index++] = static_cast<Output>(pter.Value()[jj]);
}
++pter;
}
}
template <typename TInputMesh>
template <typename Output>
void
MeshFileWriter<TInputMesh>::CopyCellsToBuffer(Output * data)
{
// Get input mesh pointer
const typename InputMeshType::CellsContainer * cells = this->GetInput()->GetCells();
// Define required variables
typename TInputMesh::PointIdentifier const * ptIds;
typename TInputMesh::CellType * cellPtr;
// For each cell
SizeValueType index = NumericTraits<SizeValueType>::ZeroValue();
typename TInputMesh::CellsContainerConstIterator cter = cells->Begin();
while (cter != cells->End())
{
cellPtr = cter.Value();
// Write the cell type
switch (cellPtr->GetType())
{
case CellGeometryEnum::VERTEX_CELL:
data[index++] = static_cast<Output>(CellGeometryEnum::VERTEX_CELL);
break;
case CellGeometryEnum::LINE_CELL:
data[index++] = static_cast<Output>(CellGeometryEnum::LINE_CELL);
break;
case CellGeometryEnum::TRIANGLE_CELL:
data[index++] = static_cast<Output>(CellGeometryEnum::TRIANGLE_CELL);
break;
case CellGeometryEnum::QUADRILATERAL_CELL:
data[index++] = static_cast<Output>(CellGeometryEnum::QUADRILATERAL_CELL);
break;
case CellGeometryEnum::POLYGON_CELL:
data[index++] = static_cast<Output>(CellGeometryEnum::POLYGON_CELL);
break;
case CellGeometryEnum::TETRAHEDRON_CELL:
data[index++] = static_cast<Output>(CellGeometryEnum::TETRAHEDRON_CELL);
break;
case CellGeometryEnum::HEXAHEDRON_CELL:
data[index++] = static_cast<Output>(CellGeometryEnum::HEXAHEDRON_CELL);
break;
case CellGeometryEnum::QUADRATIC_EDGE_CELL:
data[index++] = static_cast<Output>(CellGeometryEnum::QUADRATIC_EDGE_CELL);
break;
case CellGeometryEnum::QUADRATIC_TRIANGLE_CELL:
data[index++] = static_cast<Output>(CellGeometryEnum::QUADRATIC_TRIANGLE_CELL);
break;
default:
itkExceptionMacro(<< "Unknown mesh cell");
}
// The second element is number of points for each cell
data[index++] = cellPtr->GetNumberOfPoints();
// Others are point identifiers in the cell
ptIds = cellPtr->GetPointIds();
unsigned int numberOfPoints = cellPtr->GetNumberOfPoints();
for (unsigned int ii = 0; ii < numberOfPoints; ++ii)
{
data[index++] = static_cast<Output>(ptIds[ii]);
}
++cter;
}
}
template <typename TInputMesh>
template <typename Output>
void
MeshFileWriter<TInputMesh>::CopyPointDataToBuffer(Output * data)
{
const typename InputMeshType::PointDataContainer * pointData = this->GetInput()->GetPointData();
// typename TInputMesh::PixelType value = NumericTraits< typename
// TInputMesh::PixelType >::ZeroValue();
// TODO? NumericTraitsVariableLengthVectorPixel should define ZeroValue()
// Should define NumericTraitsArrayPixel
unsigned int numberOfComponents =
MeshConvertPixelTraits<typename TInputMesh::PixelType>::GetNumberOfComponents(pointData->Begin().Value());
SizeValueType index = 0;
typename TInputMesh::PointDataContainer::ConstIterator pter = pointData->Begin();
while (pter != pointData->End())
{
for (unsigned int jj = 0; jj < numberOfComponents; ++jj)
{
data[index++] =
static_cast<Output>(MeshConvertPixelTraits<typename TInputMesh::PixelType>::GetNthComponent(jj, pter.Value()));
}
++pter;
}
}
template <typename TInputMesh>
template <typename Output>
void
MeshFileWriter<TInputMesh>::CopyCellDataToBuffer(Output * data)
{
const typename InputMeshType::CellDataContainer * cellData = this->GetInput()->GetCellData();
// typename TInputMesh::CellPixelType value = NumericTraits< typename
// TInputMesh::CellPixelType >::ZeroValue();
// TODO? NumericTraitsVariableLengthVectorPixel should define ZeroValue()
// Should define NumericTraitsArrayPixel
unsigned int numberOfComponents =
MeshConvertPixelTraits<typename TInputMesh::CellPixelType>::GetNumberOfComponents(cellData->Begin().Value());
SizeValueType index = 0;
typename TInputMesh::CellDataContainer::ConstIterator cter = cellData->Begin();
while (cter != cellData->End())
{
for (unsigned int jj = 0; jj < numberOfComponents; ++jj)
{
data[index++] = static_cast<Output>(
MeshConvertPixelTraits<typename TInputMesh::CellPixelType>::GetNthComponent(jj, cter.Value()));
}
++cter;
}
}
template <typename TInputMesh>
void
MeshFileWriter<TInputMesh>::PrintSelf(std::ostream & os, Indent indent) const
{
Superclass::PrintSelf(os, indent);
os << indent << "File Name: " << (m_FileName.data() ? m_FileName.data() : "(none)") << std::endl;
os << indent << "Mesh IO: ";
if (m_MeshIO.IsNull())
{
os << "(none)\n";
}
else
{
os << m_MeshIO << "\n";
}
if (m_UseCompression)
{
os << indent << "Compression: On\n";
}
else
{
os << indent << "Compression: Off\n";
}
if (m_FactorySpecifiedMeshIO)
{
os << indent << "FactorySpecifiedMeshIO: On\n";
}
else
{
os << indent << "FactorySpecifiedMeshIO: Off\n";
}
}
} // end namespace itk
#endif