ITK  6.0.0
Insight Toolkit
itkMeshFileWriter.h
Go to the documentation of this file.
1/*=========================================================================
2 *
3 * Copyright NumFOCUS
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0.txt
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *=========================================================================*/
18#ifndef itkMeshFileWriter_h
19#define itkMeshFileWriter_h
20
22#include "itkProcessObject.h"
23#include "itkMeshIOBase.h"
24
25namespace itk
26{
27
51template <typename TInputMesh>
52class ITK_TEMPLATE_EXPORT MeshFileWriter : public ProcessObject
53{
54public:
55 ITK_DISALLOW_COPY_AND_MOVE(MeshFileWriter);
56
62
64 itkNewMacro(Self);
65
67 itkOverrideGetNameOfClassMacro(MeshFileWriter);
68
70 using InputMeshType = TInputMesh;
73 using InputMeshPixelType = typename InputMeshType::PixelType;
74 using InputMeshCellType = typename InputMeshType::CellType;
76
78 using Superclass::SetInput;
79 void
80 SetInput(const InputMeshType * input);
81
82 const InputMeshType *
84
85 const InputMeshType *
86 GetInput(unsigned int idx);
87
89 itkSetStringMacro(FileName);
90 itkGetStringMacro(FileName);
102 void
104 {
105 if (this->m_MeshIO != io)
106 {
107 this->Modified();
108 this->m_MeshIO = io;
109 }
110 m_FactorySpecifiedMeshIO = false;
111 m_UserSpecifiedMeshIO = true;
112 }
113 itkGetModifiableObjectMacro(MeshIO, MeshIOBase);
116 void
118 {
119 m_FileTypeIsBINARY = false;
120 }
121 void
123 {
124 m_FileTypeIsBINARY = true;
125 }
126
130 virtual void
132
135 void
136 Update() override
137 {
138 this->Write();
139 }
140
142 itkSetMacro(UseCompression, bool);
143 itkGetConstReferenceMacro(UseCompression, bool);
144 itkBooleanMacro(UseCompression);
147protected:
148 MeshFileWriter() = default;
149 ~MeshFileWriter() override = default;
150 void
151 PrintSelf(std::ostream & os, Indent indent) const override;
152
153 template <typename Output>
154 void
155 CopyPointsToBuffer(Output * data);
156
157 template <typename Output>
158 void
159 CopyCellsToBuffer(Output * data);
160
161 template <typename Output>
162 void
163 CopyPointDataToBuffer(Output * data);
164
165 template <typename Output>
166 void
167 CopyCellDataToBuffer(Output * data);
168
169 void
171
172 void
174
175 void
177
178 void
180
181private:
182 std::string m_FileName{};
183 MeshIOBase::Pointer m_MeshIO{ nullptr };
184 bool m_UserSpecifiedMeshIO{ false }; // track whether the MeshIO is user specified
185 bool m_FactorySpecifiedMeshIO{ false }; // track whether the factory mechanism set the MeshIO
186 bool m_UseCompression{ false };
187 bool m_FileTypeIsBINARY{ false };
188};
189
190
195template <typename TMeshPointer>
196ITK_TEMPLATE_EXPORT void
197WriteMesh(TMeshPointer && mesh, const std::string & filename, bool compress = false)
198{
199 using NonReferenceMeshPointer = std::remove_reference_t<TMeshPointer>;
200 static_assert(std::is_pointer_v<NonReferenceMeshPointer> || mpl::IsSmartPointer<NonReferenceMeshPointer>::Value,
201 "WriteMesh requires a raw pointer or SmartPointer.");
204 using MeshType = std::remove_const_t<std::remove_reference_t<decltype(*mesh)>>;
205 auto writer = MeshFileWriter<MeshType>::New();
206 writer->SetInput(mesh);
207 writer->SetFileName(filename);
208 writer->SetUseCompression(compress);
209 writer->Update();
210}
211
212} // end namespace itk
213
214#ifndef ITK_MANUAL_INSTANTIATION
215# include "itkMeshFileWriter.hxx"
216#endif
217
218#if defined ITK_MESHIO_FACTORY_REGISTER_MANAGER || defined ITK_IO_FACTORY_REGISTER_MANAGER
219# include "itkMeshIOFactoryRegisterManager.h"
220#endif
221
222#endif // itkMeshFileWriter_h
Control indentation during Print() invocation.
Definition: itkIndent.h:50
Light weight base class for most itk classes.
Writes mesh data to a single file.
typename InputMeshType::Pointer InputMeshPointer
typename MeshIOBase::SizeValueType SizeValueType
void SetInput(const InputMeshType *input)
const InputMeshType * GetInput()
void CopyPointDataToBuffer(Output *data)
void CopyPointsToBuffer(Output *data)
~MeshFileWriter() override=default
const InputMeshType * GetInput(unsigned int idx)
MeshFileWriter()=default
void PrintSelf(std::ostream &os, Indent indent) const override
void CopyCellDataToBuffer(Output *data)
void SetMeshIO(MeshIOBase *io)
static Pointer New()
void Update() override
typename InputMeshType::RegionType InputMeshRegionType
typename InputMeshType::PixelType InputMeshPixelType
void CopyCellsToBuffer(Output *data)
typename InputMeshType::CellType InputMeshCellType
virtual void Write()
Abstract superclass defines mesh IO interface.
Definition: itkMeshIOBase.h:73
IdentifierType SizeValueType
Definition: itkMeshIOBase.h:89
The base class for all process objects (source, filters, mappers) in the Insight data processing pipe...
SmartPointer< Self > Pointer
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
ITK_TEMPLATE_EXPORT void WriteMesh(TMeshPointer &&mesh, const std::string &filename, bool compress=false)
class ITK_FORWARD_EXPORT ProcessObject
Definition: itkDataObject.h:41