Skip to content

Commit d1a9c2c

Browse files
committed
ENH: Replace OBJMeshIO::OpenFile() with MeshIOBase::OpenInputFile()
`MeshIOBase::OpenInputFile()` is intended to be used by other `MeshIO` classes as well.
1 parent 18d89fd commit d1a9c2c

4 files changed

Lines changed: 35 additions & 36 deletions

File tree

Modules/IO/MeshBase/include/itkMeshIOBase.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,10 @@ class ITKIOMeshBase_EXPORT MeshIOBase : public LightProcessObject
640640
void
641641
AddSupportedWriteExtension(const char * extension);
642642

643+
/** Opens the input file specified by the current FileName */
644+
[[nodiscard]] std::ifstream
645+
OpenInputFile() const;
646+
643647
/** Read data from input file stream to buffer with ascii style */
644648
template <typename T>
645649
void

Modules/IO/MeshBase/src/itkMeshIOBase.cxx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*=========================================================================*/
1818

1919
#include "itkMeshIOBase.h"
20+
#include "itksys/SystemTools.hxx"
2021

2122
namespace itk
2223
{
@@ -227,4 +228,30 @@ MeshIOBase::PrintSelf(std::ostream & os, Indent indent) const
227228
os << indent << "Point pixel component type: " << GetComponentTypeAsString(m_PointPixelComponentType) << std::endl;
228229
os << indent << "Cell pixel component type: " << GetComponentTypeAsString(m_CellPixelComponentType) << std::endl;
229230
}
231+
232+
233+
std::ifstream
234+
MeshIOBase::OpenInputFile() const
235+
{
236+
if (m_FileName.empty())
237+
{
238+
itkExceptionStringMacro("No input FileName");
239+
}
240+
241+
if (!itksys::SystemTools::FileExists(m_FileName.c_str()))
242+
{
243+
itkExceptionMacro("File " << m_FileName << " does not exist");
244+
}
245+
246+
// Open the input file in binary mode. (Text mode does not work well on Windows, when using tellg() and seekg().)
247+
std::ifstream inputFile(m_FileName, std::ios::binary);
248+
249+
// Test whether the file was opened
250+
if (!inputFile.is_open())
251+
{
252+
itkExceptionMacro("Unable to open file " << m_FileName);
253+
}
254+
255+
return inputFile;
256+
}
230257
} // namespace itk

Modules/IO/MeshOBJ/include/itkOBJMeshIO.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,6 @@ class ITKIOMeshOBJ_EXPORT OBJMeshIO : public MeshIOBase
177177

178178
void
179179
PrintSelf(std::ostream & os, Indent indent) const override;
180-
181-
private:
182-
[[nodiscard]] std::ifstream
183-
OpenFile() const;
184180
};
185181
} // end namespace itk
186182

Modules/IO/MeshOBJ/src/itkOBJMeshIO.cxx

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -57,34 +57,6 @@ OBJMeshIO::CanWriteFile(const char * fileName)
5757
return true;
5858
}
5959

60-
std::ifstream
61-
OBJMeshIO::OpenFile() const
62-
{
63-
if (this->m_FileName.empty())
64-
{
65-
itkExceptionStringMacro("No input FileName");
66-
}
67-
68-
if (!itksys::SystemTools::FileExists(m_FileName.c_str()))
69-
{
70-
itkExceptionMacro("File " << this->m_FileName << " does not exist");
71-
}
72-
73-
// Read file as ascii
74-
// Due to the windows couldn't work well for tellg() and seekg() for ASCII
75-
// mode, hence we
76-
// open the file with std::ios::binary
77-
std::ifstream inputFile(m_FileName, std::ios::binary);
78-
79-
// Test whether the file was opened
80-
if (!inputFile.is_open())
81-
{
82-
itkExceptionMacro("Unable to open file " << this->m_FileName);
83-
}
84-
85-
return inputFile;
86-
}
87-
8860

8961
bool
9062
OBJMeshIO::SplitLine(const std::string & line, std::string & type, std::string & content)
@@ -118,7 +90,7 @@ void
11890
OBJMeshIO::ReadMeshInformation()
11991
{
12092
// Define input file stream and attach it to input file
121-
std::ifstream inputFile = OpenFile();
93+
std::ifstream inputFile = MeshIOBase::OpenInputFile();
12294

12395
// Read and analyze the first line in the file
12496
SizeValueType numberOfCellPoints = 0;
@@ -188,7 +160,7 @@ void
188160
OBJMeshIO::ReadPoints(void * buffer)
189161
{
190162
// Define input file stream and attach it to input file
191-
std::ifstream inputFile = OpenFile();
163+
std::ifstream inputFile = MeshIOBase::OpenInputFile();
192164

193165
// Number of data array
194166
auto * data = static_cast<float *>(buffer);
@@ -219,7 +191,7 @@ void
219191
OBJMeshIO::ReadCells(void * buffer)
220192
{
221193
// Define input file stream and attach it to input file
222-
std::ifstream inputFile = OpenFile();
194+
std::ifstream inputFile = MeshIOBase::OpenInputFile();
223195

224196
// Read and analyze the first line in the file
225197
const auto data = make_unique_for_overwrite<long[]>(this->m_CellBufferSize - this->m_NumberOfCells);
@@ -273,7 +245,7 @@ void
273245
OBJMeshIO::ReadPointData(void * buffer)
274246
{
275247
// Define input file stream and attach it to input file
276-
std::ifstream inputFile = OpenFile();
248+
std::ifstream inputFile = MeshIOBase::OpenInputFile();
277249

278250
// Number of data array
279251
auto * data = static_cast<float *>(buffer);

0 commit comments

Comments
 (0)