ITK  5.4.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();
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{};
184  bool m_UserSpecifiedMeshIO{}; // track whether the MeshIO is
185  // user specified
186  bool m_FactorySpecifiedMeshIO{}; // track whether the factory
187  // mechanism set the MeshIO
188  bool m_UseCompression{};
189  bool m_FileTypeIsBINARY{};
190 };
191 
192 
197 template <typename TMeshPointer>
198 ITK_TEMPLATE_EXPORT void
199 WriteMesh(TMeshPointer && mesh, const std::string & filename, bool compress = false)
200 {
201  using NonReferenceMeshPointer = std::remove_reference_t<TMeshPointer>;
202  static_assert(std::is_pointer_v<NonReferenceMeshPointer> || mpl::IsSmartPointer<NonReferenceMeshPointer>::Value,
203  "WriteMesh requires a raw pointer or SmartPointer.");
206  using MeshType = std::remove_const_t<std::remove_reference_t<decltype(*mesh)>>;
207  auto writer = MeshFileWriter<MeshType>::New();
208  writer->SetInput(mesh);
209  writer->SetFileName(filename);
210  writer->SetUseCompression(compress);
211  writer->Update();
212 }
213 
214 } // end namespace itk
215 
216 #ifndef ITK_MANUAL_INSTANTIATION
217 # include "itkMeshFileWriter.hxx"
218 #endif
219 
220 #if defined ITK_MESHIO_FACTORY_REGISTER_MANAGER || defined ITK_IO_FACTORY_REGISTER_MANAGER
221 # include "itkMeshIOFactoryRegisterManager.h"
222 #endif
223 
224 #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:199
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: itkAnnulusOperator.h:24
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