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 
25 namespace itk
26 {
27 
51 template <typename TInputMesh>
52 class ITK_TEMPLATE_EXPORT MeshFileWriter : public ProcessObject
53 {
54 public:
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 *
83  GetInput();
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
131  Write();
132 
135  void
136  Update() override
137  {
138  this->Write();
139  }
140 
142  itkSetMacro(UseCompression, bool);
143  itkGetConstReferenceMacro(UseCompression, bool);
144  itkBooleanMacro(UseCompression);
147 protected:
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
170  WritePoints();
171 
172  void
173  WriteCells();
174 
175  void
176  WritePointData();
177 
178  void
179  WriteCellData();
180 
181 private:
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 
195 template <typename TMeshPointer>
196 ITK_TEMPLATE_EXPORT void
197 WriteMesh(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
Pointer
SmartPointer< Self > Pointer
Definition: itkAddImageFilter.h:93
itk::MeshFileWriter::InputMeshPixelType
typename InputMeshType::PixelType InputMeshPixelType
Definition: itkMeshFileWriter.h:73
itk::MeshFileWriter
Writes mesh data to a single file.
Definition: itkMeshFileWriter.h:52
itk::MeshIOBase::SizeValueType
IdentifierType SizeValueType
Definition: itkMeshIOBase.h:89
itk::MeshIOBase
Abstract superclass defines mesh IO interface.
Definition: itkMeshIOBase.h:72
itk::MeshFileWriter::InputMeshRegionType
typename InputMeshType::RegionType InputMeshRegionType
Definition: itkMeshFileWriter.h:72
itk::SmartPointer< Self >
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::MeshFileWriter::InputMeshCellType
typename InputMeshType::CellType InputMeshCellType
Definition: itkMeshFileWriter.h:74
itkMeshFileWriterException.h
itk::MeshFileWriter::Update
void Update() override
Definition: itkMeshFileWriter.h:136
itk::MeshFileWriter::SetMeshIO
void SetMeshIO(MeshIOBase *io)
Definition: itkMeshFileWriter.h:103
itk::LightObject
Light weight base class for most itk classes.
Definition: itkLightObject.h:55
itkProcessObject.h
itk::MeshFileWriter::SizeValueType
typename MeshIOBase::SizeValueType SizeValueType
Definition: itkMeshFileWriter.h:75
itk::GTest::TypedefsAndConstructors::Dimension2::RegionType
ImageBaseType::RegionType RegionType
Definition: itkGTestTypedefsAndConstructors.h:54
itk::MeshFileWriter::InputMeshType
TInputMesh InputMeshType
Definition: itkMeshFileWriter.h:70
itk::WriteMesh
ITK_TEMPLATE_EXPORT void WriteMesh(TMeshPointer &&mesh, const std::string &filename, bool compress=false)
Definition: itkMeshFileWriter.h:197
itk::MeshFileWriter::SetFileTypeAsBINARY
void SetFileTypeAsBINARY()
Definition: itkMeshFileWriter.h:122
itk::MeshFileWriter::New
static Pointer New()
itkMeshIOBase.h
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnatomicalOrientation.h:29
itk::ProcessObject
The base class for all process objects (source, filters, mappers) in the Insight data processing pipe...
Definition: itkProcessObject.h:139
itk::ProcessObject
class ITK_FORWARD_EXPORT ProcessObject
Definition: itkDataObject.h:41
itk::MeshFileWriter::SetFileTypeAsASCII
void SetFileTypeAsASCII()
Definition: itkMeshFileWriter.h:117
itk::MeshFileWriter::InputMeshPointer
typename InputMeshType::Pointer InputMeshPointer
Definition: itkMeshFileWriter.h:71